// DK02ControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DK02Control_MFC.h"
#include "DK02ControlDlg.h"
#include "device_detection.h"
#include "mxdk02.h"
#include "Service.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



#define NUMBER_OF_SLOTS		4
#define	REGISTER_OFFSET		4

#define DK02_DRIVER_NAME	"mxdk02.sys"
#define DK02_SERVICE_NAME	"mxdk02"
#define DK02_DISPLAY_NAME	"mxdk02 Service"

/*Global variables*/
int		radio_button_enable_ids[] = {IDC_SLOT_CONTROL11,IDC_SLOT_CONTROL21,IDC_SLOT_CONTROL31,IDC_SLOT_CONTROL41};
int		radio_button_disable_ids[] = {IDC_SLOT_CONTROL12,IDC_SLOT_CONTROL22,IDC_SLOT_CONTROL32,IDC_SLOT_CONTROL42};

char	dir_path[MAX_PATH];
char	file_path[MAX_PATH];
//CService svc;

// CAboutDlg dialog used for App About

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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
//	virtual BOOL DestroyWindow();
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CDK02ControlDlg dialog




CDK02ControlDlg::CDK02ControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDK02ControlDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDK02ControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDK02ControlDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_APPLY_SETTING, &CDK02ControlDlg::OnBnClickedApplySetting)
END_MESSAGE_MAP()


// CDK02ControlDlg message handlers

BOOL CDK02ControlDlg::OnInitDialog()
{
	
	int			slot_status = 0;	
	DWORD		ret = 0;
	HRSRC		hResource;
	HGLOBAL		glob;
	BYTE		*buffer;
	int			size;
	FILE		*file_ptr;	
	DWORD		Property;
	BOOL		bDeviceFound = false;

	CDeviceDetection	device;
	CButton				*radio_button;	

	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
	
	UNREFERENCED_PARAMETER(glob);
	UNREFERENCED_PARAMETER(size);
	UNREFERENCED_PARAMETER(hResource);
	UNREFERENCED_PARAMETER(buffer);
	UNREFERENCED_PARAMETER(file_ptr);

	/*Initialize radio button status*/
	update_radio_button_status();

	/*hide the radio button according to EPM-3337*/
	Property=SPDRP_DEVICEDESC;
	device.InitParam("PCI",Property,DeviceDesc_AtherosWiFi);

	device.SearchDevices();

	bDeviceFound = device.IsDeviceFound();

	if(bDeviceFound==TRUE){
		//MessageBox("AR922x found","Info",MB_OK);
		
		Property=SPDRP_LOCATION_INFORMATION;

		device.InitParam("PCI",Property,DEVICE_SECOND_SLOT);

		device.SearchDevices();

		bDeviceFound = device.IsDeviceFound();
		if(bDeviceFound==TRUE){
			//MessageBox("EPM-3337 on second slot","Info",MB_OK);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL31);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL32);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL41);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL42);
			radio_button->EnableWindow(0);
		}
		
		device.InitParam("PCI",Property,DEVICE_FIRST_SLOT);

		device.SearchDevices();

		bDeviceFound = device.IsDeviceFound();
		if(bDeviceFound==TRUE){
			//MessageBox("EPM-3337 on first slot","Info",MB_OK);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL11);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL12);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL21);
			radio_button->EnableWindow(0);
			radio_button = (CButton*)GetDlgItem(IDC_SLOT_CONTROL22);
			radio_button->EnableWindow(0);
		}
	}
	

//INIT_ERROR:
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDK02ControlDlg::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 CDK02ControlDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDK02ControlDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CDK02ControlDlg::OnBnClickedApplySetting()
{
	CButton*	radio_enable;
	int			slot_status = 0;	
	int			i = 0;
	int			fd;
	int			slot_num;
	int			val;

	// TODO: Add your control notification handler code here
	/*Set the slot status*/
	for(i=0;i<NUMBER_OF_SLOTS;i++){
		/*Get current selection*/
		radio_enable = (CButton*)GetDlgItem(radio_button_enable_ids[i]);
		val = radio_enable->GetCheck();
		val = val ^ (~(i & 0x1));
		slot_num = i+1;
		
		/*Open device handle*/
		fd = mxdk02_open();
		if (fd == 0 ) {
			MessageBox("mxdk02_open fail","Error",MB_OK);
			return;
		}

		/*Set slot status*/
		if(mxdk02_set_slot(fd, slot_num, val)){
			MessageBox("mxdk02_set_slot fail","Error",MB_OK);
		}

		/*Close device*/
		mxdk02_close(fd);
	}
	
	/*Update the radio button status*/
	update_radio_button_status();
}



void CDK02ControlDlg::update_radio_button_status(void)
{
	CButton*	radio_enable;
	CButton*	radio_disable;
	int			slot_status = 0;	
	int			i = 0;
	int			fd;
	int			slot_num;
		
	for(i=0;i<4;i++){
		slot_num = i+1;

		fd = mxdk02_open();
		
		slot_status = mxdk02_get_slot(fd,slot_num);

		/*Close device*/
		mxdk02_close(fd);

		slot_status = slot_status ^ (~i & 0x1);
		if(slot_status){
			radio_enable = (CButton*)GetDlgItem(radio_button_enable_ids[i]);
			radio_enable->SetCheck(BST_CHECKED);
		}
		else{
			radio_disable = (CButton*)GetDlgItem(radio_button_disable_ids[i]);
			radio_disable->SetCheck(BST_CHECKED);
		}
	}
}

BOOL CDK02ControlDlg::DestroyWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	//MessageBox("Destroy Window","Info",MB_OK);

	return CDialog::DestroyWindow();
}
