#ifndef RFID_INTERFACE_H
#define RFID_INTERFACE_H

#include "sl2x23.h"
#include "iso14443a.h"
#include "iso14443b.h"
#include "iso14443_4.h"
#include "mifare.h"

#define REQA_CMD      0x26
#define WAKEUPA_CMD   0x52

#define REQB_CMD      0x00
#define WAKEUPB_CMD   0x08
//Type A
#define COM_PKT_CMD_INIT_TYPEA                  0x1E
#define COM_PKT_CMD_INIT_TYPEB                  0x1F
#define COM_PKT_CMD_REQA                        0x20
#define COM_PKT_CMD_TYPEA_HALT                  0x21
#define COM_PKT_CMD_TYPEA_MF1_READ              0x22
#define COM_PKT_CMD_TYPEA_MF1_WRITE             0x23
#define COM_PKT_CMD_TYPEA_MF1_WALLET_INIT       0x24
#define COM_PKT_CMD_TYPEA_MF1_WALLET_INCREMENT  0x25
#define COM_PKT_CMD_TYPEA_MF1_WALLET_DECREMENT  0x26
#define COM_PKT_CMD_TYPEA_MF1_WALLET_READ       0x27
#define COM_PKT_CMD_TYPEA_MF0_READ              0x28
#define COM_PKT_CMD_TYPEA_MF0_WRITE             0x29
#define COM_PKT_CMD_TYPEA_RATS                  0x2A
#define COM_PKT_CMD_EXCHANGE                    0x2B
#define COM_PKT_CMD_DESELECT                    0x2C
#define COM_PKT_CMD_MULTI_EXCHANGE_TEST         0x2D
#define COM_PKT_CMD_TEST_STOP                   0x2E
//TYPE B
#define COM_PKT_CMD_REQB                        0x30
#define COM_PKT_CMD_TYPEB_HALT                  0x31
#define COM_PKT_CMD_TYPEB_SR176_READ            0x32
#define COM_PKT_CMD_TYPEB_SR176_WRITE           0x33
#define COM_PKT_CMD_SELECT_SR                   0x34
#define COM_PKT_CMD_PROTECT_SR176               0x35
#define COM_PKT_CMD_COMPLECT_SR176              0x36
#define COM_PKT_CMD_TYPEB_UID                   0x37

#define UID_4  4
#define UID_7  7
#define UID_10 10

#define PICC_CID 0x00 // 0~14 随意指定

typedef struct tag_info
{
    uint8_t opt_step;
    uint8_t uid_length;
    uint8_t tag_type;
    uint8_t tag_type_bytes[2];
    uint8_t serial_num[12];
    uint8_t uncoded_key[6];
} tag_info;

typedef struct statistics_t
{
    uint32_t reqa_cnt;
    uint32_t reqa_fail;
    uint32_t reqb_cnt;
    uint32_t reqb_fail;
    uint32_t write_fail;
    uint32_t read_fail;
    uint32_t check_fail;
    uint32_t atqa_fail;
    uint32_t cos_fail;
    uint32_t atqa_cnt;
    uint32_t cos_cnt;
    uint32_t lpcd_cnt;
    uint32_t lpcd_fail;
} statistics_t;

extern statistics_t  g_statistics;
extern tag_info  g_tag_info;
extern uint8_t  g_loop_buf[60];//不支持太大的cos指令测试
extern uint8_t  g_statistic_refreshed;
extern uint8_t g_ask_block_flag;


void Rfid_Init(void);
void rfid_operation(uint8_t *pcmd);
int8_t com_reqa(uint8_t *pcmd);
int8_t com_reqb(uint8_t *pcmd);

void com_exchange(uint8_t * pcmd);
void com_typea_rats(uint8_t * pcmd);
void Rfid_Close(void);
int8_t nfc_demo(void);

int8_t reqb_wakeup(uint8_t *guid);
//int8_t Detect_ContactlessCard(void);
uint16_t iISO14443TransMit(uint8_t* pbInData, uint16_t dwInLen, uint8_t* pbOutData, uint16_t* pwOutLen);

int8_t pcd_polling(uint8_t *guid);
int8_t pcd_active(uint8_t rate);
void Iblock_getrand(uint8_t rate);

void NFC_A_Mifare_Test(uint8_t *uid);
void NFC_A_Tag1_Test(uint8_t *uid);
void NFC_A_Tag2_Test(uint8_t *uid);
void NFC_F_Tag3_Test(uint8_t *uid);
void NFC_V_Tag5_Test(uint8_t *uid);
int8_t pcd_request_f(uint8_t *uid) ;
int8_t pcd_request_v(uint8_t *uid);
void slm_arm(uint8_t type);
#endif
