// *** mf0xAB.h, header for mifare API, Ver. 1.7, Windows ***
// *** based on rd0xAB.h / rd0xAB.cpp, Ver. 1.7, Windows ***
#ifndef _MF0XAB_WIN_H_
#define _MF0XAB_WIN_H_

#include "rd0xAB.h"
// ATTENTION! See rd0xAB.h to change DLL_API (_RD0XAB_DLL_)

#define SZ_ATS                  30

// *** convert uidlen, atqa, sak to card type ***
DLL_API BYTE acq2type(DWORD acq, BYTE atslen, PBYTE ats);

// *** DIR-mode initialization ***
DLL_API DWORD mfInit(PCCR pccr, PBYTE pmodel, PBYTE phw, PDWORD psn);

// *** beep, tone = 0..5 ***
DLL_API DWORD mfBeep(PCCR pccr, BYTE tone);

// *** carrier break ***
DLL_API DWORD mfBrkCarrier(PCCR pccr, WORD time_ms);

// *** card type detection ***
DLL_API DWORD mfType(PCCR pccr, BYTE timeout, PBYTE ptype, PBYTE puidlen, PBYTE puid, PWORD patqa, PBYTE psak, PBYTE patslen, PBYTE pats);

// *** select card ***
DLL_API DWORD mfSelect(PCCR pccr, BYTE timeout, BYTE rq, PBYTE puidlen, PBYTE puid, PWORD patqa, PBYTE psak);

// *** set authorization key ***
DLL_API DWORD mfSetAK(PCCR pccr, BYTE ak, PBYTE key);

// *** data block authorization only then card halted ***
DLL_API DWORD mfAuth(PCCR pccr, BYTE timeout,
    BYTE acm, BYTE ak, BYTE block, PBYTE puidlen, PBYTE puid, PWORD patqa, PBYTE psak);

// *** send long command and receive answer ***
DLL_API DWORD mfLong(PCCR pccr, PBYTE txbuf, BYTE txlen, PBYTE ans, PBYTE pabitlen);

// *** calculate CRC-A by buf ***
DLL_API WORD mfCalcCrcA(PBYTE buf, DWORD len);

// *** calculate CRC-A and append buf ***
DLL_API void mfAppendCrcA(PBYTE buf, DWORD len);

// *** basis function for card IO ***
DLL_API DWORD mfIO(PCCR pccr, PBYTE buf, BYTE len, PBYTE par, PBYTE ans, PBYTE pabitlen, PBYTE anspar);

// *** basis function for card IO (bit based)***
DLL_API DWORD mfBitIO(PCCR pccr, PBYTE buf, BYTE blen, PBYTE ans, PBYTE pabitlen, PBYTE anspar);

// *** read data block/page ***
// pccr    - pointer to the handle struct (see link_hidopen or link_comopen)
// timeout - operation timeout 0..7, time_in_ms=(timeout*4+3)*25; min=75mS, max=775mS
// mfdm    - data mode: MFDM_BLOCK | MFDM_PAGE (block/page mode), MFDM_NMC | MFDM_RWC (normal/RW card)
// acm     - authorization command (PICC_AUTHENT1A | PICC_AUTHENT1B): no authorization mode when acm=0
//  ak     - authorization key 0..84, RAM-key when ak=0
// start   - start block or page for read 0..255
// len     - data length in blocks or pages
// pdata   - a pointer to the data buffer, may be NULL for 'dummy' read operation
// puidlen - may be NULL; before call: length of UID in puid for force operation with UID = puid ONLY!
//           puidlen must be NULL or *puidlen must be 0 if need 'any UID' mode
//           after call: the length of the UID card used
// puid    - may be NULL; before call: UID for force operation with card UID = puid ONLY!
//           after call: UID of last used card
// patqa   - may be NULL; after call: ATQA of last used card
// psak    - may be NULL; after call: SAK of last used card
// return  - error code
DLL_API DWORD mfDataRead(PCCR pccr, BYTE timeout, BYTE mfdm, BYTE acm, BYTE ak, \
    BYTE start, BYTE len, PBYTE pdata, PBYTE puidlen, PBYTE puid, PWORD patqa, PBYTE psak);

// *** write data block/page ***
// pccr    - pointer to the handle struct (see link_hidopen or link_comopen)
// timeout - operation timeout 0..7, time_in_ms=(timeout*4+3)*25; min=75mS, max=775mS
// mfdm    - data mode: MFDM_BLOCK | MFDM_PAGE (block/page mode), MFDM_NMC | MFDM_RWC (normal/RW card)
// acm     - authorization command (PICC_AUTHENT1A | PICC_AUTHENT1B): no authorization mode when acm=0
//  ak     - authorization key 0..84, RAM-key when ak=0
// start   - start block or page for read 0..255
// len     - data length in blocks or pages
// pdata   - a pointer to the data buffer, can't be NULL
// puidlen - may be NULL; before call: length of UID in puid for force operation with UID = puid ONLY!
//           puidlen must be NULL or *puidlen must be 0 if need 'any UID' mode
//           after call: the length of the UID card used
// puid    - may be NULL; before call: UID for force operation with card UID = puid ONLY!
//           after call: UID of last used card
// patqa   - may be NULL; after call: ATQA of last used card
// psak    - may be NULL; after call: SAK of last used card
// return  - error code
DLL_API DWORD mfDataWrite(PCCR pccr, BYTE timeout, BYTE mfdm, BYTE acm, BYTE ak, \
    BYTE start, BYTE len, PBYTE pdata, PBYTE puidlen, PBYTE puid, PWORD patqa, PBYTE psak);

// *** calculate parity bit***
#define mfOddParity(bt) ((BYTE)((0x9669 >> ((bt ^ (bt >> 4)) & 0x0F)) & 1))

#endif  // _MF0XAB_WIN_H_
