/*
 * APP_IDcard.c
 *
 *  Created on: Sep 6, 2021
 *      Author: wuwenfeng
 */
#include "APP_IDcard.h"

window *idcard_window;
char idcard_str[32];

void APP_IDcard_init(window *a_window)
{
	idcard_window=a_window;
	RC522_Init();
}
unsigned char CT[2];
unsigned char SN[4];
unsigned char RFID[16];
unsigned char status;
unsigned char s=0x08;
char KEYA[6]={0,0,0,0,0,0};
char KEYB[6]={0xff,0xff,0xff,0xff,0xff,0xff};
uint32_t card_number_D=0;

char fundcard,PA,sele,checkkey,read_flag;

//通过debug返回卡片数据

void APP_IDcard_loop()
{
	fundcard = PcdRequest(PICC_REQALL,CT);
	if(fundcard==MI_OK)
	{

		 status=MI_ERR;
		 PA = PcdAnticoll(SN);
		 if(PA==MI_OK)
		 {
			 printf("\n***********************\n");
			 printf("Card Type:%X%X\n",CT[0],CT[1]);
			 printf("CardId:%X%X%X%X\n",SN[0],SN[1],SN[2],SN[3]);

			 sprintf(idcard_str,"CardId:%X%X",CT[0],CT[1]);
			 LCD_ShowString(idcard_window->x, idcard_window->y+16, idcard_str, 16, GREEN, RED);
			 sprintf(idcard_str,"CardId:%X%X%X%X",SN[0],SN[1],SN[2],SN[3]);
			 LCD_ShowString(idcard_window->x, idcard_window->y+32, idcard_str, 16, GREEN, RED);

			 sele=PcdSelect(SN);
			 if(sele==MI_OK)
			 {

					 LCD_ShowString(idcard_window->x, idcard_window->y+48, "Key ok", 16, GREEN, RED);
					 for(int a=0;a<64;a++)
					 {
						 checkkey=PcdAuthState(0x61,a,KEYB,SN);
						 if(checkkey==MI_OK)
						 {
							 read_flag=PcdRead(a,RFID);
							 if(read_flag==MI_OK)
							 {
								 for(int d=0;d<16;d++)
								 {
									 printf("%02X",RFID[d]);
								 }
							 }else
							 {
								 printf("READ_ERROR");
							 }

					     }else
					     {
					    	 printf("KEY_ERROR");
					     }
						 printf("\n");
					 }



			 }

		 }
	}
	/*
	if (status==MI_OK)
	{
		status=MI_ERR;
		card_number_D=(SN[2]<<16)+(SN[1]<<8)+(SN[0]);
		sprintf(idcard_str,"CardId:%d",card_number_D);
		LCD_ShowString(idcard_window->x, idcard_window->y+16, idcard_str, 16, GREEN, RED);

		status=MI_ERR;
		status =PcdAuthState(0x61,0,KEY,SN);
	}
	if(status==MI_OK)
	{
		LCD_ShowString(idcard_window->x, idcard_window->y+32, "KEY_OK", 16, GREEN, RED);
		status=MI_ERR;
		status=PcdRead(0,RFID);
	}
	 if(status==MI_OK)
	 {
		 sprintf(idcard_str,"%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x",RFID[0],RFID[1],RFID[2],RFID[3],RFID[4],RFID[5],RFID[6],RFID[7],RFID[8],RFID[9],RFID[10],RFID[11],RFID[12],RFID[13],RFID[14],RFID[15]);
		 LCD_ShowString(idcard_window->x, idcard_window->y+32, idcard_str, 16, GREEN, RED);
	 }
*/

}

