// ISO14443_4_ExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ISO14443_4_Example.h"
#include "ISO14443_4_ExampleDlg.h"
#include "MasterRD.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define BAUD_NUMBER  7
const long BaudValue[BAUD_NUMBER] ={ 9600,14400,19200,28800,38400,57600,115200};
const CString BaudStr[BAUD_NUMBER] = { _T("9600"), _T("14400"), _T("19200"), _T("28800"), _T("38400"), _T("57600"), _T("115200")};
/////////////////////////////////////////////////////////////////////////////
// MainDlg dialog

MainDlg::MainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(MainDlg::IDD, pParent)
	, m_hInst(NULL)
	, m_nReaderID(0)
	, m_nPort(1)
	,m_nType(0)
{
	//{{AFX_DATA_INIT(MainDlg)
	m_strRstInfo = _T("");
	m_strRetInfo = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void MainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(MainDlg)
	DDX_Control(pDX, IDC_EDIT_RET_INFO, m_editRetInfo);
	DDX_Control(pDX, IDC_EDIT_COS_CMD, m_editCOSCMD);
	DDX_Control(pDX, IDC_EDIT_RST_INFO, m_editRSTInfo);
	DDX_Control(pDX, IDC_COMBO_RAUD, m_combBaud);
	DDX_Control(pDX, IDC_COMBO_PORT, m_combPort);
	DDX_Text(pDX, IDC_EDIT_ICDEV, m_nReaderID);
	DDX_Radio(pDX, IDC_RADIO_TYPE_A, m_nType);
	DDX_Text(pDX, IDC_EDIT_RST_INFO, m_strRstInfo);
	DDX_Text(pDX, IDC_EDIT_RET_INFO, m_strRetInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(MainDlg, CDialog)
	//{{AFX_MSG_MAP(MainDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_COMBO_PORT, OnSelchangeComboPortBaud)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BTN_RST, OnBtnRst)
	ON_CBN_SELCHANGE(IDC_COMBO_RAUD, OnSelchangeComboPortBaud)
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MainDlg message handlers

BOOL MainDlg::OnInitDialog()
{
	int i, state;
	CString strt;
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	MasterRD_Load_rfLib(&m_hInst);
	for (i = 0; i < BAUD_NUMBER; ++i)
	{
		m_combBaud.AddString(BaudStr[i]);
	}
	m_combBaud.SetCurSel(2);
	for (i = 1; i < 10; ++i)
	{
		strt.Format(_T("COM %d"), i);
		m_combPort.AddString(strt);
	}
	m_combPort.SetCurSel(0);
	m_nPort = 1;

	state = rf_COM_Open(1, 19200);
	
	if(state != LIB_SUCCESS)
	{
		rf_COM_Close(1);			
		MessageBox("Can't open reader!","Error",MB_OK|MB_ICONERROR);
		//return FALSE;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void MainDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR MainDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void MainDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void MainDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void MainDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	if (m_hInst != NULL)
	{
		MasterRD_Free_rfLib(&m_hInst);
	}
	// TODO: Add your message handler code here
	
}

void MainDlg::OnButtonConnect() 
{
	UpdateData();
	CString str;
	int states = LIB_SUCCESS;
	states = rf_Reader_ComOpen(m_nPort, m_nReaderID);
	if (LIB_SUCCESS != states )	
	{
		if (LIB_READER_EXIST == states)
		{
			MessageBox(_T("The Reader has been Opened!"), _T("Error"), MB_OK|MB_ICONEXCLAMATION);
			return;
		}
		MessageBox(_T("rf_Reader_ComOpen Error"),_T("Error"),MB_OK|MB_ICONERROR);
		SetWindowText(_T("No Reader"));
		return;
	}
	str.Format(_T("Connected Reader %d @ COM%d"), m_nReaderID, m_nPort);
	SetWindowText(str);

	// ÉèÖÃ¶Á¿¨Æ÷ÎªISO15693Ä£Ê½
	rf_Reader_SetISOType(m_nReaderID, 0x31);
}

void MainDlg::OnSelchangeComboPortBaud()
{

	int nBaud = BaudValue[m_combBaud.GetCurSel()];
	int nPort = m_combPort.GetCurSel() + 1;
	
	//close reader.
    rf_COM_Close(m_nPort);

	m_nPort = nPort;
	//reopen reader.
	int state = 1;	
    state = rf_COM_Open(nPort,nBaud);
	if(state != LIB_SUCCESS)
	{
		//rf_ClosePort(m_icdev);			
		MessageBox("Can't open reader!","Error",MB_OK|MB_ICONERROR);
		return ;
	}	
}

void MainDlg::Str2HexStr( CString &strHex, BYTE* pcInStr, int nLen )
{
	CString str;
	int i;
	for (i = 0; i < nLen; ++i)
	{
		str.Format(_T("%02X"), pcInStr[i]);
		strHex += str;
	}
}

void MainDlg::OnBtnRst() 
{
	int nState;
	BYTE pcBuf[512];
	BYTE cLen = 0;
	if (!rf_Reader_IsOpen(m_nReaderID))
	{
		MessageBox(_T("Please Connect a Reader first!"), _T("Err"));
		return;
	}
	UpdateData();
	if (m_nType == 0) // type A
	{
		nState = rf_ISO14443A_GetCard(m_nReaderID, 0x52, pcBuf, &cLen);
		if (nState != LIB_SUCCESS)
		{
			m_strRetInfo += _T("rf_ISO14443A_GetCard Error!");
			m_strRetInfo += _T("\r\n");
			m_strRstInfo.Empty();
			UpdateData(FALSE);
			m_editRetInfo.SetSel(m_strRetInfo.GetLength(), m_strRetInfo.GetLength());
			return;
		}
		nState = rf_ISO14443_4_TypeA_RATS(m_nReaderID, pcBuf, &cLen);
		if (nState != LIB_SUCCESS)
		{
			m_strRetInfo += _T("rf_ISO14443_4_TypeA_RATS Error!");
			m_strRetInfo += _T("\r\n");
			m_strRstInfo.Empty();
			UpdateData(FALSE);
			m_editRetInfo.SetSel(m_strRetInfo.GetLength(), m_strRetInfo.GetLength());
			return;
		}
		rf_Reader_SetBeep(m_nReaderID, 5);
		rf_Reader_SetLight(m_nReaderID, 2);
		m_strRstInfo.Empty();
		Str2HexStr(m_strRstInfo, pcBuf, cLen);
		UpdateData(FALSE);		
	}
	else if (m_nType == 1) // type B
	{
		nState = rf_ISO14443B_GetCard(m_nReaderID, 1, 0, 0, pcBuf);
		if (nState != LIB_SUCCESS)
		{
			m_strRetInfo += _T("rf_ISO14443B_GetCard Error!");
			m_strRetInfo += _T("\r\n");
			m_strRstInfo.Empty();
			UpdateData(FALSE);
			m_editRetInfo.SetSel(m_strRetInfo.GetLength(), m_strRetInfo.GetLength());
			return;
		}
		rf_Reader_SetBeep(m_nReaderID, 5);
		rf_Reader_SetLight(m_nReaderID, 2);

		m_strRstInfo.Empty();
		Str2HexStr(m_strRstInfo, pcBuf, 12);
		UpdateData(FALSE);		
	}
	
}

void MainDlg::OnBtnSend() 
{
	int nState;
	BYTE pcBuf[512];
	BYTE cLen = 0;
	if (!rf_Reader_IsOpen(m_nReaderID))
	{
		MessageBox(_T("Please Connect a Reader first!"), _T("Err"));
		return;
	}
	UpdateData();
	CString strCMD;
	m_editCOSCMD.GetWindowTextEx(strCMD);
	if (strCMD.IsEmpty())
	{
		MessageBox(_T("COS CMD is empty!"));
		return;
	}
	nState = rf_ISO14443_4_COS(m_nReaderID, (BYTE*)strCMD.GetBuffer(strCMD.GetLength()), strCMD.GetLength(), pcBuf, &cLen, 2000);
	if (nState != LIB_SUCCESS)
	{
		m_strRetInfo += _T("rf_ISO14443_4_COS Error!");
	}
	else
	{
		Str2HexStr(m_strRetInfo, pcBuf, cLen);
	}
	rf_Reader_SetBeep(m_nReaderID, 5);
	m_strRetInfo += _T("\r\n");
	UpdateData(FALSE);		
	m_editRetInfo.SetSel(m_strRetInfo.GetLength(), m_strRetInfo.GetLength());
}
