📄 mifare511.h
字号:
#ifdef __cplusplus
extern "C" {
#endif
#include <unistd.h>
/*! \ingroup error
* RCLSTATUS is a signed short value, using the positive range. \n
* High byte: Category (group) Identifier. \n
* Low byte : Error Specifier.
*/
typedef short RCLSTATUS;
/* S U C C E S S */
/*! \name Success
\ingroup error */
/*@{*/
#define RCL_SUCCESS (0x0000) /*!< Returned in case of no error when there
isn't any more appropriate code. */
/*@}*/
/* C O M M U N I C A T I O N */
/*! \name Communication Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_IO_TIMEOUT (0x0001) /*!< No reply received, e.g. PICC removal. */
#define RCL_CRC_ERROR (0x0002) /*!< Wrong CRC detected by RC or library. */
#define RCL_PARITY_ERROR (0x0003) /*!< Parity error detected by RC or library. */
#define RCL_BITCOUNT_ERROR (0x0004) /*!< Typically, the RC reports such an error. */
#define RCL_FRAMING_ERROR (0x0005) /*!< Invalid frame format. */
#define RCL_COLLISION_ERROR (0x0006) /*!< Typically, the RC repors such an error. */
#define RCL_BUFFER_TOO_SMALL (0x0007) /*!< Communication buffer size insufficient. */
#define RCL_ACCESS_DENIED (0x0008) /*!< Access has not been granted (readonly?). */
#define RCL_BUFFER_OVERFLOW (0x0009) /*!< Attempt to write beyond the end of a
buffer. */
#define RCL_RF_ERROR (0x000A) /*!< RF is not switched on in time in active
communication mode. */
#define RCL_PROTOCOL_ERROR (0x000B) /*!< Mifare start bit wrong, buffer length
error. */
#define RCL_ERROR_NY_IMPLEMENTED (0x000C) /*!< Feature not yet implemented. */
#define RCL_FIFO_WRITE_ERROR (0x000D) /*!< Error caused because of interface conflict
during write access to FIFO. */
#define RCL_USERBUFFER_FULL (0x000E) /*!< The user buffer is full, the calling
application/routine gets the chance to
save user buffer data and start over. */
#define RCL_RESET_NEEDED (0x00CC)
/*@}*/
/* I N T E R F A C E (Device as well as function parameters) errors: */
/*! \name Interface Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_INVALID_PARAMETER (0x0101) /*!< Parameter is invalid (range, format). */
#define RCL_UNSUPPORTED_PARAMETER (0x0102) /*!< Parameter value/format is correct but not
supported in the current configuration. */
#define RCL_UNSUPPORTED_COMMAND (0x0103) /*!< The device does not support the command. */
#define RCL_INTERFACE_ERROR (0x0104) /*!< Host-peripheral interface error. */
#define RCL_INVALID_FORMAT (0x0105) /*!< The data format does not match the spec. */
#define RCL_INTERFACE_NOT_ENABLED (0x0106) /*!< This interface is currently(!) not
supported (e.g. function ptr. to NULL). */
#define RCL_UNKNOWN_HARDWARE_TYPE (0x0107) /*!< The chosen hardware for configurations is
not known by the initialisation function.
The default hardware (HW_1) is used instead. */
/*@}*/
/* M F errors: */
/*! \name Mifare Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_AUTHENT_ERROR (0x0201) /*!< Authentication failure (e.g. key
mismatch). */
#define RCL_ACK_SUPPOSED (0x0202) /*!< Single byte or nibble received, CRC error
detected, possibly MF (N)ACK response. */
#define RCL_NACK_RECEIVED (0x0203) /*!< NACK detected */
/*@}*/
/* I S O 1 4 4 4 3 . 4 Level specific errors: */
/*! \name ISO 14443-4 Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_BLOCKNR_NOT_EQUAL (0x0301) /*!< Frame OK, but Blocknumber mismatch. */
/*@}*/
/* N F C Errors and Stati: */
/*! \name NFC Errors
\ingroup error */
/*@{*/
#define RCL_TARGET_DEADLOCKED (0x0401) /*!< Target has not sent any data, but RF
(generated by the Target) is still switched on. */
#define RCL_TARGET_SET_TOX (0x0402) /*!< Target has sent Timeout Extension Request. */
#define RCL_TARGET_RESET_TOX (0x0403) /*!< Reset timeout-value after Timeout Extension. */
#define RCL_TARGET_DESELECTED (0x0404) /*!< Target has been deselected (used in the dispatcher) */
#define RCL_TARGET_RELEASED (0x0405) /*!< Target has been released (used in the dispatcher) */
#define RCL_TARGET_NFCID_MISMATCH (0x0406) /*!< The NFCID2 does not match withthe NFCID3 during
activation in passive communication mode above 106
kbps. (used in the dispatcher) */
/*@}*/
/* I S O 1 4 4 4 3 . 3 Level specific errors: */
/*! \name ISO 14443-3 Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_WRONG_UID_CHECKBYTE (0x0501) /*!< UID check byte is wrong. */
#define RCL_WRONG_HALT_FORMAT (0x0502) /*!< HALT Format error. */
/*@}*/
/* I D M A N A G E R specific errors: */
/*! \name ID-Manager Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_ID_ALREADY_IN_USE (0x0601) /*!< ID cannot be assigned because it is already used. */
#define RCL_INSTANCE_ALREADY_IN_USE (0x0602) /*!< INSTANCE cannot be assigned because it is already used. */
#define RCL_ID_NOT_IN_USE (0x0603) /*!< Specified ID is not in use. */
#define RCL_NO_ID_AVAILABLE (0x0604) /*!< No ID is available, all are occupied. */
/*@}*/
/* Felica Errors and Stati: */
/*! \name NFC Errors
\ingroup error */
/*@{*/
#define RCL_FELICA_PACK_LEN (0x0701) // pack exchange length error
#define RCL_FELICA_RESP_CODE (0x0702) // pack exchange responce code error
#define RCL_FELICA_RESP_DATA_ERROR (0x0703) // pack exchange responce data error
#define RCL_FELICA_WRONG_POLLING_RESP (0x0710) // polling responce error
/* Mifare Errors and Stati: */
#define RCL_TYPEA_ATQ_LEN (0x0901) // pack exchange length error
#define RCL_TYPEA_ANTICOLL_LEN (0x0902) // anticoll pack exchange length error
#define RCL_TYPEA_ANTICOLL_CHKSUM (0x0903) // anticoll checksum error
#define RCL_TYPEA_SEL_LEN (0x0904) // select pack exchange length error
#define RCL_MFC_READ_LEN (0x0910) // mifare read 16 pack exchange length error
#define RCL_MFC_READ_NAK (0x0911) // mifare read 16 NAK
#define RCL_MFC_READ_STRANGE (0x0912) // mifare read 16 strange responce from card
#define RCL_MFC_WRITE_NAK (0x0913) // mifare write NAK
#define RCL_MFC_WRITE_STRANGE (0x0914) // mifare write strange responce from card
#define RCL_MFC_VALUE_NAK (0x0915) // mifare Dec/Inc NAK
#define RCL_MFC_VALUE_INVAL (0x0916) // mifare Dec/Inc invalid value block
#define RCL_MFC_VALUE_STRANGE (0x0917) // mifare Dec/Inc strange responce from card
/* O T H E R E R R O R S : */
/*! \name Other Errors/Status Values
\ingroup error */
/*@{*/
#define RCL_OTHER_ERROR (0x7E01) /*!< Unspecified, error, non-categorised. */
#define RCL_INSUFFICIENT_RESOURCES (0x7E02) /*!< The system runs low of resources! */
#define RCL_INVALID_DEVICE_STATE (0x7E03) /*!< The (sub-)system is in a state which
does not allow the operation. */
#define RCL_JOINER_TEMP_ERROR (0x7E04) /*!< Temperature error indicated by Joiner HW.*/
/*@}*/
#define RCO_VAL_RF106K (0x00) /*<! RF UART speed setting for 106 kbps. Might be used for Tx and
Rx configuration. */
#define RCO_VAL_RF212K (0x01) /*<! RF UART speed setting for 212 kbps. Might be used for Tx and
Rx configuration. */
#define RCO_VAL_RF424K (0x02) /*<! RF UART speed setting for 424 kbps. Might be used for Tx and
Rx configuration. */
#define RCO_VAL_RF848K (0x03) /* */
#define RCO_VAL_RF1M6 (0x04) /* */
#define RCO_VAL_RF3M2 (0x05) /* */
/* EOF */
#define PICC_REQIDL 0x26 //!< request idle
#define PICC_REQALL 0x52 //!< request all
#define PICC_ANTICOLL1 0x93 //!< anticollision level 1 106 kBaud
#define PICC_ANTICOLL11 0x92 //!< anticollision level 1 212 kBaud
#define PICC_ANTICOLL12 0x94 //!< anticollision level 1 424 kBaud
#define PICC_ANTICOLL13 0x98 //!< anticollision level 1 848 kBaud
#define PICC_ANTICOLL2 0x95 //!< anticollision level 2
#define PICC_ANTICOLL3 0x97 //!< anticollision level 3
#define PICC_AUTHENT1A 0x60 //!< authentication(证明,鉴定) using key A
#define PICC_AUTHENT1B 0x61 //!< authentication(证明,鉴定) using key B
#define PICC_READ16 0x30 //!< read 16 byte block
#define PICC_WRITE16 0xA0 //!< write 16 byte block
#define PICC_WRITE4 0xA2 //!< write 4 byte block
#define PICC_DECREMENT 0xC0 //!< decrement value
#define PICC_INCREMENT 0xC1 //!< increment value
#define PICC_RESTORE 0xC2 //!< restore command code
#define PICC_TRANSFER 0xB0 //!< transfer command code
#define PICC_HALT 0x50 //!< halt
#define MI_OK 0
extern unsigned long Set511AsMifare(unsigned char RxSpeedIdx, unsigned char TxSpeedIdx);
extern unsigned long TypeAREQ(unsigned char req_code, unsigned char * pATQA);
extern unsigned long TypeACascAntiCollOne(unsigned char SelCode, unsigned char * pnr);
extern unsigned long TypeACascSelOne(unsigned char SelCode, unsigned char * pnr, unsigned char * SAK);
extern unsigned long TypeAHaltA(void);
extern unsigned long MFCAuthKey(
unsigned char auth_mode,
unsigned char *snr,
unsigned char *keys,
unsigned char block);
extern unsigned long MFCRead16Bytes(unsigned char sect, unsigned char * pbuff);
extern unsigned long MFCWrite4Bytes(unsigned char sect, unsigned char * pbuff);
extern unsigned long MFCWrite16Bytes(unsigned char sect, unsigned char * pbuff);
extern unsigned long MFCDecInc(unsigned char sect, unsigned char isInc, unsigned char * vbuff);
extern unsigned long MFCRestore(unsigned char sect);
extern unsigned long MFCTransfer(unsigned char sect);
#define MFCDecrement(a,b) MFCDecInc((a), 0, (b))
#define MFCIncrement(a,b) MFCDecInc((a), 1, (b))
extern int OpenRC(void);
extern void CloseRC(void);
static inline int InitMifare(void) {
if (OpenRC() != MI_OK) {
CloseRC();
return -1;
}
usleep(100000);
return Set511AsMifare(RCO_VAL_RF106K, RCO_VAL_RF106K);
}
static inline void MifareOff(void) {
CloseRC();
}
#define CardTypeARequest(a,b) TypeAREQ((a),(b))
#define CardTypeAAnticoll(a,b) TypeACascAntiCollOne((a),(b))
#define CardTypeAAnticollLevel1(a) TypeACascAntiCollOne(PICC_ANTICOLL1,(a))
#define CardTypeASelect(a,b,c) TypeACascSelOne((a), (b), (c))
#define CardTypeASelectLevel1(b,c) TypeACascSelOne(PICC_ANTICOLL1, (b), (c))
#define CardTypeAHalt TypeAHaltA
#define CardMFCRead16Bytes(a,b) MFCRead16Bytes((a),(b))
#define CardMFCWrite16Bytes(a,b) MFCWrite16Bytes((a),(b))
#define CardMFCAuthKey(a,b,c,d) MFCAuthKey((a),(b),(c),(d))
static inline int CardMFCValueWithTransfer(
unsigned char dd_mode,
unsigned char from_block,
unsigned char to_block,
unsigned long lvalue
) {
int rcode = -1;
if (dd_mode == PICC_INCREMENT)
rcode = MFCIncrement(from_block, (unsigned char*)&lvalue);
else if (dd_mode == PICC_DECREMENT)
rcode = MFCDecrement(from_block, (unsigned char*)&lvalue);
else if (dd_mode == PICC_RESTORE)
rcode = MFCRestore(from_block);
if (rcode)
return rcode;
return MFCTransfer(to_block);
}
static inline int CardMFCValueDebit(
unsigned char dd_mode,
unsigned char block_addr,
unsigned long lvalue
) {
int rcode = -1;
if (dd_mode == PICC_INCREMENT)
rcode = MFCIncrement(block_addr, (unsigned char*)&lvalue);
else if (dd_mode == PICC_DECREMENT)
rcode = MFCDecrement(block_addr, (unsigned char*)&lvalue);
else if (dd_mode == PICC_RESTORE)
rcode = MFCRestore(block_addr);
return rcode;
}
#ifdef __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -