/*
 * 	Module_RFID.h
 *
 *  Created on: 2019-10-24
 *  Update: 2020-10-19
 *  Author: Eason Yang
 *	Version: V0.03
 *
 * History
 * 1. Added : Reyax constant.
 *
 *
 *
 *
 *
 */
 
#ifndef LIB_RFID_H_
#define LIB_RFID_H_

#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/types.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.h>
#include 	<arpa/inet.h>
#include 	<netinet/in.h>

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>	/* Standard input/output definitions */
#include    <stdlib.h>
#include    <unistd.h>	/* UNIX standard function definitions */
#include    <fcntl.h>	/* File control definitions */
#include    <termios.h>	/* POSIX terminal control definitions */
#include    <errno.h>	/* Error number definitions */
#include 	<string.h>	/* String function definitions */
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include	<stdbool.h>

typedef struct Rfid
{
	uint8_t     snType;             // 4 bytes or 6 bytes or 7 bytes or 10 bytes
	uint8_t		cardType;			// 0: NONE 1: ISO14443A  2: ISO14443B  4: FELICA
	uint8_t		currentCard[32];    // Current charging cycle start card number
	uint8_t     targetBlock;        // RFID read/write target block
	uint8_t     keyType;            // 0: Key-A  1: Key-B
	uint8_t		data_Block[32];     // RFID operation block data
}RFID;

//==================================
// Global Constant (Card Length)
//==================================
#define RFID_SN_TYPE_4BYTE		4
#define RFID_SN_TYPE_6BYTE		6
#define RFID_SN_TYPE_7BYTE      7
#define RFID_SN_TYPE_10BYTE     10

//==================================
// Global Constant (Card Type)
//==================================
#define NONE				0
#define ISO14443A			1
#define IS014443B			2
#define FELICA				3

//==================================
// Global Constant (Module type)
//==================================
#define NO_MODULE			-1
#define MODULE_EWT			0
#define MODULE_REYAX		1

//==================================
// Global Constant (RF Output level)
//==================================
#define SET_RF_STRONGEST	0x00
#define SET_RF_STRONGER		0x01
#define SET_RF_WEAK			0x02
#define SET_RF_WEAKEST		0x03

//==================================
// Global Constant (Automatic Detecting)
//==================================
#define SET_DETECTING_OFF			0x00
#define SET_DETECTING_ON			0x01
#define SET_DETECTING_TEMPORARILY	0x11

//==========================
// Declare RFID module type
//==========================
#define RFID_MODULE_TYPE	MODULE_EWT		// EWT : MODULE_EWT	REYAX : MODULE_REYAX

char ClaCheckSum(unsigned char *buffer, int len);
int system_command(int uart, unsigned char* cmd,int length, unsigned char* rx);
int at_command(int uart, unsigned char* cmd,int length, unsigned char* rx);
bool getRequestCardSN(int Fd, int moduleType,RFID *rfid);
bool ISO14443A_REQUEST_SN(int Fd, int moduleType, unsigned char *serialNumber);
bool ISO14443B_REQUEST_SN(int Fd, int moduleType, unsigned char *serialNumber);
bool FELICA_REQUEST_SN(int Fd, int moduleType, unsigned char *serialNumber);
bool getBlockRead(int Fd, int moduleType,int block, unsigned char keyid, unsigned char *data);
bool setBlockWrite(int Fd, int moduleType,int block, unsigned char keyid, unsigned char *data);
bool setDefaultOfAutomaticDetectingCard(int Fd, int moduleType, int Value);
bool setRfOutputLevel(int Fd, int moduleType, int Value);
void setBuzzer(int Fd, int moduleType, unsigned char time);
void sethaltCard(int Fd, int moduleType);

#endif