// ReadWriteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ReadWrite.h"
#include "ReadWriteDlg.h"
#include "mwrf32.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE icdev=NULL;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadWriteDlg dialog

CReadWriteDlg::CReadWriteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReadWriteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReadWriteDlg)
	m_strKey = _T("");
	m_strData = _T("");
	m_BlockNr = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReadWriteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReadWriteDlg)
	DDX_Control(pDX, IDC_EDIT_DATA, m_DataCtrl);
	DDX_Control(pDX, IDC_EDIT_KEY, m_KeyCtrl);
	DDX_Text(pDX, IDC_EDIT_KEY, m_strKey);
	DDX_Text(pDX, IDC_EDIT_DATA, m_strData);
	DDX_Text(pDX, IDC_EDIT_SECTOR, m_BlockNr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReadWriteDlg, CDialog)
	//{{AFX_MSG_MAP(CReadWriteDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
	ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)
	ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
	ON_BN_CLICKED(IDC_BTN_WRITE, OnBtnWrite)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadWriteDlg message handlers

BOOL CReadWriteDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
    m_BlockNr = 4;
    m_strKey.Format("FFFFFFFFFFFF");
	UpdateData(FALSE);
	m_KeyCtrl.LimitText(12);
	m_DataCtrl.LimitText(32);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReadWriteDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CReadWriteDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CReadWriteDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CReadWriteDlg::OnBtnConnect() 
{
    int port=0;
	long baud=0;
	long baudlist[]={9600,115200};

	icdev=Open_USB();
	if((int)icdev>0)
	    MessageBox("连接成功！");
	else
		MessageBox("连接失败！");
	
}

void CReadWriteDlg::OnBtnDisconnect() 
{
    int st=0;
	st=Close_USB(icdev);
	if(st)
		MessageBox("断开连接失败！");
	else
		MessageBox("断开连接成功！");
}


void CReadWriteDlg::OnBtnRead() 
{
	int st=0;
	unsigned __int16 TagType=0;
	unsigned char _size=0;
	unsigned long snr=0;
	char data[33];
	unsigned char key[7];

	memset(data, 0, 33);
	UpdateData();

	if(m_BlockNr<0 || m_BlockNr>63)
	{
		MessageBox("块地址不正确！");
		return;
	}
	if(m_strKey.GetLength()!=12)
	{
		MessageBox("密码不正确！");
	}
	memset(key, 0, 7);
	a_hex(m_strKey.GetBuffer(12), key, 12);
	m_strKey.ReleaseBuffer();
	st=rf_request(icdev, 1, &TagType);
	if(st)
	{
		MessageBox("request error!");
		return;
	}
	st=rf_anticoll(icdev, 0, &snr);
	if(st)
	{
		MessageBox("anticoll error!");
		return;
	}
	st=rf_select(icdev, snr, &_size);
	if(st)
	{
		MessageBox("select error!");
		return;
	}
	st=rf_authentication_key(icdev, 0, m_BlockNr, key);
	if(st)
	{
		MessageBox("authentication error!");
		return;
	}
	st=rf_read_hex(icdev, m_BlockNr, data);
	if(st==0)
		m_strData.Format("%s", data);
	else
		m_strData.Format("");
	st=rf_halt(icdev);
	UpdateData(FALSE);
	MessageBox("读数据成功！");
}

void CReadWriteDlg::OnBtnWrite() 
{
	int st=0;
	unsigned __int16 TagType=0;
	unsigned char _size=0;
	unsigned long snr=0;
	unsigned char key[7];
	char data[33];

    memset(data, 0, 33);
	UpdateData();
	
	if(m_BlockNr<0 || m_BlockNr>63)
	{
		MessageBox("块地址不正确！");
		return;
	}
	if(m_strKey.GetLength()!=12)
	{
		MessageBox("密码不正确！");
	}
	if(m_strData.GetLength()!=32)
	{
		MessageBox("数据长度不正确！");
		return;
	}
	
	memset(key, 0, 7);
	a_hex(m_strKey.GetBuffer(12), key, 12);
	m_strKey.ReleaseBuffer();
	st=rf_request(icdev, 1, &TagType);
	if(st)
	{
		MessageBox("request error!");
		return;
	}
	st=rf_anticoll(icdev, 0, &snr);
	if(st)
	{
		MessageBox("anticoll error!");
		return;
	}
	st=rf_select(icdev, snr, &_size);
	if(st)
	{
		MessageBox("select error!");
		return;
	}
	st=rf_authentication_key(icdev, 0, m_BlockNr, key);
	if(st)
	{
		MessageBox("authentication error!");
		return;
	}
	memcpy(data, m_strData.GetBuffer(32), 32);
	m_strData.ReleaseBuffer();
	st=rf_write_hex(icdev, m_BlockNr, data);
	if(st)
	{
		MessageBox("写数据失败！");
		return;
	}
	st=rf_halt(icdev);	
	MessageBox("写数据成功！");
}
