#include <string.h>
#include "mifare.h"
#include "Hal_Reg.h"
#include "StatusCode.h"
#include "TypeA.h"

//  write NTAG203,144bytes
uint8_t write_NFC_Info(char *data)
{
	uint8_t ATQ[2];
	uint8_t SAK;
	uint8_t Glbsnr[10];
	uint8_t i;
	char status;
	if(PiccRequest(	REQIDLE, ATQ ) ==	MI_OK){
		if(	PiccCascAnticoll(	SELECT_CASCADE_LEVEL_1,	0, Glbsnr)	== MI_OK ){
			if(	PiccCascSelect(	SELECT_CASCADE_LEVEL_1,	Glbsnr, &SAK	)	== MI_OK ){
				if(SAK & 0x04){		//  序列号没有读完
					*Glbsnr = *(Glbsnr + 1);
					*(Glbsnr + 1) = *(Glbsnr + 2);
					*(Glbsnr + 2) = *(Glbsnr + 3);
					status = PiccCascAnticoll(0x95,0,Glbsnr+3);	// level 2
					if(!status){
						status	=	PiccCascSelect(0x95, Glbsnr+3,	&SAK);
						for(i = 0; i < 36; i++){
							PiccULWrite(4 + i, (unsigned char *)&data[4 * i]);
						}
						return 1;
					}
				}
			}
		}
	}
	else{
		if(PiccRequest(	REQIDLE, ATQ ) ==	MI_OK){
			if(	PiccCascAnticoll(	SELECT_CASCADE_LEVEL_1,	0, Glbsnr)	== MI_OK ){
				if(	PiccCascSelect(	SELECT_CASCADE_LEVEL_1,	Glbsnr, &SAK	)	== MI_OK ){
					if(SAK & 0x04){		//  序列号没有读完
						*Glbsnr = *(Glbsnr + 1);
						*(Glbsnr + 1) = *(Glbsnr + 2);
						*(Glbsnr + 2) = *(Glbsnr + 3);
						status = PiccCascAnticoll(0x95,0,Glbsnr+3);	// level 2
						if(!status){
							status	=	PiccCascSelect(0x95, Glbsnr+3,	&SAK);
							for(i = 0; i < 36; i++){
								PiccULWrite(4 + i, (unsigned char *)&data[4 * i]);
							}
							return 1;
						}
					}
				}
			}
		}
	}
	return 0;
}

//  read NTAG203,144bytes
uint8_t read_NFC_Info(char* data)
{
	uint8_t ATQ[2];
	uint8_t SAK;
	uint8_t Glbsnr[10];
	uint8_t i;
	char status;
	if(PiccRequest(	REQIDLE, ATQ ) ==	MI_OK){
		if(	PiccCascAnticoll(	SELECT_CASCADE_LEVEL_1,	0, Glbsnr)	== MI_OK ){
			if(	PiccCascSelect(	SELECT_CASCADE_LEVEL_1,	Glbsnr, &SAK	)	== MI_OK ){
				if(SAK & 0x04){		//  序列号没有读完
					*Glbsnr = *(Glbsnr + 1);
					*(Glbsnr + 1) = *(Glbsnr + 2);
					*(Glbsnr + 2) = *(Glbsnr + 3);
					status = PiccCascAnticoll(0x95,0,Glbsnr+3);	// level 2
					if(!status){
						status	=	PiccCascSelect(0x95, Glbsnr+3,	&SAK);
						for(i = 0; i < 36; i++){
							PiccRead(4 + i, (unsigned char *)&data[4 * i]);
						}
						return 1;
					}
				}
			}
		}
	}
	else{
		if(PiccRequest(	REQIDLE, ATQ ) ==	MI_OK){
			if(	PiccCascAnticoll(	SELECT_CASCADE_LEVEL_1,	0, Glbsnr)	== MI_OK ){
				if(	PiccCascSelect(	SELECT_CASCADE_LEVEL_1,	Glbsnr, &SAK	)	== MI_OK ){
					if(SAK & 0x04){		//  序列号没有读完
						*Glbsnr = *(Glbsnr + 1);
						*(Glbsnr + 1) = *(Glbsnr + 2);
						*(Glbsnr + 2) = *(Glbsnr + 3);
						status = PiccCascAnticoll(0x95,0,Glbsnr+3);	// level 2
						if(!status){
							status	=	PiccCascSelect(0x95, Glbsnr+3,	&SAK);
							for(i = 0; i < 36; i++){
								PiccRead(4 + i, (unsigned char *)&data[4 * i]);
							}
							return 1;
						}
					}
				}
			}
		}
	}
	return 0;


}	

void RC522Init(void)
{
	uint8_t temp = 0;
	uint8_t data[144];
	char info[] = "sawyer 18001126984.";
	i2c_init();
	iic_Read_reg(0x37,&temp,1);
	while( (temp != 0x92) && (temp != 0x91) ){
		iic_Read_reg(0x37,&temp,1);
	}
	PcdConfig();
	memcpy(data, info, strlen(info));
//	while( write_NFC_Info(data) != 1 ){}
}

uint8_t read_NFC_Card(char* data)
{
	uint8_t ATQ[2];
	uint8_t SAK;
	uint8_t Glbsnr[4];
	if( PiccRequest( REQALL, ATQ ) == MI_OK ){
		if( PiccCascAnticoll( SELECT_CASCADE_LEVEL_1, 0, Glbsnr ) == MI_OK ){
			if( PiccCascSelect( SELECT_CASCADE_LEVEL_1, Glbsnr, &SAK ) == MI_OK ){
				data[0] = (((Glbsnr[0] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[0] / 16;
				data[1] = (((Glbsnr[0] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[0] % 16;
				data[2] = ' ';
				data[3] = (((Glbsnr[1] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[1] / 16;
				data[4] = (((Glbsnr[1] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[1] % 16;
				data[5] = ' ';
				data[6] = (((Glbsnr[2] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[2] / 16;
				data[7] = (((Glbsnr[2] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[2] % 16;
				data[8] = ' ';
				data[9] = (((Glbsnr[3] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[3] / 16;
				data[10] = (((Glbsnr[3] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[3] % 16;
				data[11] = '.';
				data[12] = '\0';
				return 0;
			}
		}
	} 
	else {
			if( PiccRequest( REQALL, ATQ ) == MI_OK ){
				if( PiccCascAnticoll( SELECT_CASCADE_LEVEL_1, 0, Glbsnr ) == MI_OK ){
					if( PiccCascSelect( SELECT_CASCADE_LEVEL_1, Glbsnr, &SAK ) == MI_OK ){
						data[0] = (((Glbsnr[0] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[0] / 16;
						data[1] = (((Glbsnr[0] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[0] % 16;
						data[2] = ' ';
						data[3] = (((Glbsnr[1] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[1] / 16;
						data[4] = (((Glbsnr[1] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[1] % 16;
						data[5] = ' ';
						data[6] = (((Glbsnr[2] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[2] / 16;
						data[7] = (((Glbsnr[2] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[2] % 16;
						data[8] = ' ';
						data[9] = (((Glbsnr[3] / 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[3] / 16;
						data[10] = (((Glbsnr[3] % 16) < 10) ? '0' : ('A' - 10)) + Glbsnr[3] % 16;
						data[11] = '.';
						data[12] = '\0';
						return 0;
					}
				}
			}
	}
	memcpy(data, "can't read. ", 13);
	return 1;
}



