#include "stdafx.h"
#include "WiFiClient.h"
#include "WiFiClientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CString IntToHex(unsigned char val)
{
	CString s;
	s.Format(_T("%.2X"), val);
	return s;
}

bool ShowResult(const int Res)
{
	if (Res == WCL_E_SUCCESS)
		return true;
	
	CString s;
	s.Format(_T("Error: 0x%.8X"), Res);
	AfxMessageBox(s);
	return false;
}

CString GuidToString(const GUID& Id)
{
	CString res;
	
	LPOLESTR lpszClsID;
	StringFromCLSID(Id, &lpszClsID);
	res = CString(lpszClsID);
	CoTaskMemFree(lpszClsID);
	return res;
}

GUID StringToGuid(const CString& s)
{
	GUID Guid;
	CLSIDFromString((LPOLESTR)const_cast<LPTSTR>((LPCTSTR)s), &Guid);
	return Guid;
}

CString BoolToStr(const bool Val)
{
	if (Val)
		return _T("True");
	else
		return _T("False");
}

CString IntToStr(const int Val)
{
	CString s;
	s.Format(_T("%d"), Val);
	return s;
}

CString IntToStr(const long long Val)
{
	CString s;
	s.Format(_T("%d"), Val);
	return s;
}

CString GetEnumName(const wclWiFiBssType Enum)
{
	switch (Enum)
	{
		case bssInfrastructure:
			return _T("bssInfrastructure");
		case bssIndependent:
			return _T("bssIndependent");
		case bssAny:
			return _T("bssAny");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclWiFiPhy Enum)
{
	switch (Enum)
	{
		case phyAny:
			return _T("phyAny");			
		case phyFhss:
			return _T("phyFhss");
		case phyDsss:
			return _T("phyDsss");
		case phyIr:
			return _T("phyIr");
		case phyOfdm:
			return _T("phyOfdm");
		case phyHrDsss:
			return _T("phyHrDsss");
		case phyErp:
			return _T("phyErp");
		case phyHt:
			return _T("phyHt");
		case phyVht:
			return _T("phyVht");
		case phyUnknown:
			return _T("phyUnknown");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclWiFiAuthAlgorithm Enum)
{
	switch (Enum)
	{
		case auOpen:
			return _T("auOpen");
		case auSharedKey:
			return _T("auSharedKey");
		case auWpa:
			return _T("auWpa");
		case auWpaPsk:
			return _T("auWpaPsk");
		case auWpaNone:
			return _T("auWpaNone");
		case auRsna:
			return _T("auRsna");
		case auRsnaPsk:
			return _T("auRsnaPsk");
		case auWpa3:
			return _T("auWpa3");
		case auWpa3Sae:
			return _T("auWpa3Sae");
		case auOwe:
			return _T("auOwe");
		case auWpa3Ent:
			return _T("auWpa3Ent");
		case auUnknown:
			return _T("auUnknown");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclWiFiCipherAlgorithm Enum)
{
	switch (Enum)
	{
		case caNone:
			return _T("caNone");
		case caWep40:
			return _T("caWep40");
		case caTkip:
			return _T("caTkip");
		case caCcmp:
			return _T("caCcmp");
		case caWep104:
			return _T("caWep104");
		case caBip:
			return _T("caBip");
		case caGcmp128:
			return _T("caGcmp128");
		case caGcmp256:
			return _T("caGcmp256");
		case caCcmp256:
			return _T("caCcmp256");
		case caBipGmac128:
			return _T("caBipGmac128");
		case caBipGmac256:
			return _T("caBipGmac256");
		case caBipCmac256:
			return _T("caBipCmac256");
		case caUseGroup:
			return _T("caUseGroup");
		case caWep:
			return _T("caWep");
		case caUnknown:
			return _T("caUnknown");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclWiFiPowerSetting Enum)
{
	switch (Enum)
	{
		case psNo:
			return _T("psNo");
		case psLow:
			return _T("psLow");
		case psMedium:
			return _T("psMedium");
		case psMaximum:
			return _T("psMaximum");
		case psInvalid:
			return _T("psInvalid");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclWiFiInterfaceState Enum)
{
	switch (Enum)
	{
		case isConnected:
			return _T("isConnected");
		case isAdHocFormed:
			return _T("isAdHocFormed");
		case isDisconnecting:
			return _T("isDisconnecting");
		case isDisconnected:
			return _T("isDisconnected");
		case isAssociating:
			return _T("isAssociating");
		case isDiscovering:
			return _T("isDiscovering");
		case isAuthenticating:
			return _T("isAuthenticating");
		case isNotReady:
			return _T("isNotReady");
		default:
			return _T("UNKNOWN");
	}
}

CString DecodeFlags(const wclWiFiBssCaps& Flags)
{
	CString Res = _T("");
	if (Flags.find(bcEss) != Flags.end())
		Res += _T("bcEss ");
	if (Flags.find(bcIbss) != Flags.end())
		Res += _T("bcIbss ");
	if (Flags.find(bcCfPollable) != Flags.end())
		Res += _T("bcCfPollable ");
	if (Flags.find(bcCfPollRequest) != Flags.end())
		Res += _T("bcCfPollRequest ");
	if (Flags.find(bcPrivacy) != Flags.end())
		Res += _T("bcPrivacy ");
	return Res;
}

// CWiFiClientDlg dialog




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

void CWiFiClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_CLIENT_INTERFACES, lvClientInterfaces);
	DDX_Control(pDX, IDC_CHECK_CLIENT_AD_HOCK_PROFILES, cbClientAdHocProfiles);
	DDX_Control(pDX, IDC_CHECK_CLIENT_INCLUDE_HIDDEN_PROFILES, cbClientHiddenProfiles);
	DDX_Control(pDX, IDC_EDIT_CLIENT_SCAN_SSID, edClientScanSsid);
	DDX_Control(pDX, IDC_LIST_CLIENT_NETWORKS, lvClientNetworks);
	DDX_Control(pDX, IDC_CHECK_CLIENT_USE_SSID, cbClientUseSsid);
	DDX_Control(pDX, IDC_COMBO_CLIENT_BSS_TYPE, cbClientBssType);
	DDX_Control(pDX, IDC_CHECK_CLIENT_SECURITY_ENABLED, cbClientSecurityEnabled);
	DDX_Control(pDX, IDC_LIST_CLIENT_BSS, lvClientBss);
	DDX_Control(pDX, IDC_EDIT_CLIENT_NEW_PROFILE_NAME, edClientNewProfileName);
	DDX_Control(pDX, IDC_LIST_CLIENT_PROFILES, lvClientProfiles);
	DDX_Control(pDX, IDC_COMBO_CLIENT_CONNECT, cbClientConnect);
	DDX_Control(pDX, IDC_EDIT_CLIENT_PASSKEY, edClientPasskey);
	DDX_Control(pDX, IDC_CHECK_CLIENT_DISCOVERY_SECURE, cbClientDiscoverSecure);
	DDX_Control(pDX, IDC_CHECK_USE_BSS_MAC, cbUseBssMac);
	DDX_Control(pDX, IDC_CHECK_PLAIN_TEXT, cbPlainText);
}

BEGIN_MESSAGE_MAP(CWiFiClientDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_OPEN, &CWiFiClientDlg::OnBnClickedButtonClientOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_CLOSE, &CWiFiClientDlg::OnBnClickedButtonClientClose)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_ENUM_INTRFACES, &CWiFiClientDlg::OnBnClickedButtonClientEnumIntrfaces)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_ENUM_NETWORKS, &CWiFiClientDlg::OnBnClickedButtonClientEnumNetworks)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SCAN, &CWiFiClientDlg::OnBnClickedButtonClientScan)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_ENUM_BSS, &CWiFiClientDlg::OnBnClickedButtonClientEnumBss)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_DISCONNECT, &CWiFiClientDlg::OnBnClickedButtonClientDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_SHOW_DENIED_NETWORKS, &CWiFiClientDlg::OnBnClickedButtonClientGetShowDeniedNetworks)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SET_SHOW_DENIED_NETWORKS, &CWiFiClientDlg::OnBnClickedButtonClientSetShowDeniedNetworks)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_POWER_SETTING, &CWiFiClientDlg::OnBnClickedButtonClientGetPowerSetting)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_ONLY_USE_GP_PROFILES, &CWiFiClientDlg::OnBnClickedButtonClientGetOnlyUseGpProfiles)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_ALLOW_EXPLICIT_CREDS, &CWiFiClientDlg::OnBnClickedButtonClientGetAllowExplicitCreds)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SET_ALLOW_EXPLICIT_CREDS, &CWiFiClientDlg::OnBnClickedButtonClientSetAllowExplicitCreds)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_BLOCK_PERIOD, &CWiFiClientDlg::OnBnClickedButtonClientGetBlockPeriod)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SET_BLOCK_PERIOD, &CWiFiClientDlg::OnBnClickedButtonClientSetBlockPeriod)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_VIRTUAL_STATION_EXTENSIBILITY, &CWiFiClientDlg::OnBnClickedButtonClientGetVirtualStationExtensibility)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SET_VIRTUAL_STATION_EXTENSIBILITY, &CWiFiClientDlg::OnBnClickedButtonClientSetVirtualStationExtensibility)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_STATE, &CWiFiClientDlg::OnBnClickedButtonClientGetState)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_ENUM_PROFILES, &CWiFiClientDlg::OnBnClickedButtonClientEnumProfiles)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_DELETE_PROFILE, &CWiFiClientDlg::OnBnClickedButtonClientDeleteProfile)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_RENAME_PROFILE, &CWiFiClientDlg::OnBnClickedButtonClientRenameProfile)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_GET_PROFILE_XML, &CWiFiClientDlg::OnBnClickedButtonClientGetProfileXml)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_SET_PROFILE_EXML, &CWiFiClientDlg::OnBnClickedButtonClientSetProfileExml)
	ON_BN_CLICKED(IDC_BUTTON_CLIENT_CONNECT, &CWiFiClientDlg::OnBnClickedButtonClientConnect)
	ON_BN_CLICKED(IDC_BUTTON_TURN_ON, &CWiFiClientDlg::OnBnClickedButtonTurnOn)
	ON_BN_CLICKED(IDC_BUTTON_TURN_OFF, &CWiFiClientDlg::OnBnClickedButtonTurnOff)
	ON_BN_CLICKED(IDC_BUTTON_CURRENT_IP, &CWiFiClientDlg::OnBnClickedButtonCurrentIp)
	ON_BN_CLICKED(IDC_BUTTON_GET_IP_SETTINGS, &CWiFiClientDlg::OnBnClickedButtonGetIpSettings)
	ON_BN_CLICKED(IDC_BUTTON_ENABLE_DHCP, &CWiFiClientDlg::OnBnClickedButtonEnableDhcp)
	ON_BN_CLICKED(IDC_BUTTON_ENABLE_STATIC_IP, &CWiFiClientDlg::OnBnClickedButtonEnableStaticIp)
	ON_BN_CLICKED(IDC_BUTTON_SJOW_EDIT_UI, &CWiFiClientDlg::OnBnClickedButtonSjowEditUi)
	ON_BN_CLICKED(IDC_BUTTON_SET_USER_DATA, &CWiFiClientDlg::OnBnClickedButtonSetUserData)
	ON_BN_CLICKED(IDC_BUTTON1, &CWiFiClientDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &CWiFiClientDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON2, &CWiFiClientDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON4, &CWiFiClientDlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// CWiFiClientDlg message handlers

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

	// 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

	lvClientInterfaces.InsertColumn(0, _T("Interface id"), 0, 350);
	lvClientInterfaces.InsertColumn(1, _T("Description"), 0, 350);
	lvClientInterfaces.InsertColumn(2, _T("Primary"), 0, 50);
	lvClientInterfaces.SetExtendedStyle(lvClientInterfaces.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	lvClientNetworks.InsertColumn(0, _T("Profile name"), 0, 150);
	lvClientNetworks.InsertColumn(1, _T("SSID"), 0, 150);
	lvClientNetworks.InsertColumn(2, _T("BSS type"), 0, 100);
	lvClientNetworks.InsertColumn(3, _T("Number of BSS IDs"), 0, 120);
	lvClientNetworks.InsertColumn(4, _T("Connectable"), 0, 80);
	lvClientNetworks.InsertColumn(5, _T("Not connectable reason"), 0, 150);
	lvClientNetworks.InsertColumn(6, _T("PHY types"), 0, 150);
	lvClientNetworks.InsertColumn(7, _T("More PHY types"), 0, 100);
	lvClientNetworks.InsertColumn(8, _T("Signal quality"), 0, 100);
	lvClientNetworks.InsertColumn(9, _T("Security enabled"), 0, 100);
	lvClientNetworks.InsertColumn(10, _T("Def. auth. algorithm"), 0, 120);
	lvClientNetworks.InsertColumn(11, _T("Def. cipher algorithm"), 0, 120);
	lvClientNetworks.InsertColumn(12, _T("Flags"), 0, 150);
	lvClientNetworks.SetExtendedStyle(lvClientNetworks.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	cbClientBssType.InsertString(0, _T("bssInfrastructure"));
	cbClientBssType.InsertString(1, _T("bssIndependent"));
	cbClientBssType.InsertString(2, _T("bssAny"));
	cbClientBssType.SetCurSel(0);

	lvClientBss.InsertColumn(0, _T("SSID"), 0, 150);
	lvClientBss.InsertColumn(1, _T("PHY ID"), 0, 100);
	lvClientBss.InsertColumn(2, _T("MAC"), 0, 120);
	lvClientBss.InsertColumn(3, _T("BSS type"), 0, 80);
	lvClientBss.InsertColumn(4, _T("PHY type"), 0, 150);
	lvClientBss.InsertColumn(5, _T("RSSI"), 0, 120);
	lvClientBss.InsertColumn(6, _T("Link quality"), 0, 150);
	lvClientBss.InsertColumn(7, _T("In reg domain"), 0, 100);
	lvClientBss.InsertColumn(8, _T("Beacon period"), 0, 100);
	lvClientBss.InsertColumn(9, _T("Timestamp"), 0, 100);
	lvClientBss.InsertColumn(10, _T("Host timestamp"), 0, 120);
	lvClientBss.InsertColumn(11, _T("Capatbility"), 0, 120);
	lvClientBss.InsertColumn(12, _T("Center frequency"), 0, 150);
	lvClientBss.InsertColumn(13, _T("IEs"), 0, 50);
	lvClientBss.SetExtendedStyle(lvClientBss.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	lvClientProfiles.InsertColumn(0, _T("Name"), 0, 200);
	lvClientProfiles.InsertColumn(1, _T("Flags"), 0, 200);
	lvClientProfiles.SetExtendedStyle(lvClientProfiles.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	cbClientConnect.InsertString(0, _T("using network profile"));
	cbClientConnect.InsertString(1, _T("using selected profile"));
	cbClientConnect.InsertString(2, _T("using selected network"));
	cbClientConnect.InsertString(3, _T("using temporary profile"));
	cbClientConnect.SetCurSel(0);

	edClientPasskey.SetWindowText(_T("123456789"));

	cbClientDiscoverSecure.SetCheck(TRUE);

	cbPlainText.SetCheck(TRUE);

	__hook(&CwclWiFiClient::AfterOpen, &wclWiFiClient, &CWiFiClientDlg::ClientAfterOpen);
	__hook(&CwclWiFiClient::BeforeClose, &wclWiFiClient, &CWiFiClientDlg::ClientBeforeClose);

	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 CWiFiClientDlg::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 CWiFiClientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CWiFiClientDlg::OnBnClickedButtonClientOpen()
{
	ShowResult(wclWiFiClient.Open());
}

void CWiFiClientDlg::OnBnClickedButtonClientClose()
{
	ShowResult(wclWiFiClient.Close());
}

void CWiFiClientDlg::OnDestroy()
{
	CDialog::OnDestroy();

	wclWiFiClient.Close();
	__unhook(&wclWiFiClient);
}

void CWiFiClientDlg::ClientAfterOpen(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	AfxMessageBox(_T("WiFi client opened"));
	wclWiFiProfilesManager.Open();
}

void CWiFiClientDlg::ClientBeforeClose(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	wclWiFiProfilesManager.Close();
	
	ClientClearInterfaces();
	ClientClearNetworks();
	ClientClearBss();
	ClientClearProfiles();
		
	AfxMessageBox(_T("WiFi client is closing"));
}

void CWiFiClientDlg::ClientClearInterfaces()
{
	lvClientInterfaces.DeleteAllItems();
}

void CWiFiClientDlg::OnBnClickedButtonClientEnumIntrfaces()
{
	ClientClearInterfaces();

	wclWiFiInterfaces Interfaces;
	if (ShowResult(wclWiFiClient.EnumInterfaces(Interfaces)))
	{
		if (Interfaces.size() > 0)
		{
			for (unsigned int i = 0; i < Interfaces.size(); i++)
			{
				lvClientInterfaces.InsertItem(i, GuidToString(Interfaces[i].Id));
				lvClientInterfaces.SetItemText(i, 1, Interfaces[i].Description.c_str());
				lvClientInterfaces.SetItemText(i, 2, BoolToStr(Interfaces[i].Primary));
			}
		}
	}
}

bool CWiFiClientDlg::ClientGetInterfaceId(GUID& Id) const
{
	POSITION Pos = lvClientInterfaces.GetFirstSelectedItemPosition();
	if (Pos == NULL)
	{
		AfxMessageBox(_T("Select interface"));
		return false;
	}
	
	Id = StringToGuid(lvClientInterfaces.GetItemText(lvClientInterfaces.GetNextSelectedItem(Pos), 0));
	return true;
}

void CWiFiClientDlg::ClientClearNetworks()
{
	lvClientNetworks.DeleteAllItems();
}

void CWiFiClientDlg::OnBnClickedButtonClientEnumNetworks()
{
	ClientClearNetworks();
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		wclWiFiAvailableNetworkFilters Filters;
		if (cbClientAdHocProfiles.GetCheck())
			Filters.insert(ffIncludeAllAdhocProfiles);
		if (cbClientHiddenProfiles.GetCheck())
			Filters.insert(ffIncludeAllManualHiddenProfiles);
	
		wclWiFiAvailableNetworks Networks;
		if (ShowResult(wclWiFiClient.EnumAvailableNetworks(Id, Filters, Networks)))
		{
			if (Networks.size() > 0)
			{
				for (unsigned int i = 0; i < Networks.size(); i++)
				{
					CString Str;
					lvClientNetworks.InsertItem(i, Networks[i].ProfileName.c_str());
					lvClientNetworks.SetItemText(i, 1, Networks[i].Ssid.c_str());
					lvClientNetworks.SetItemText(i, 2, GetEnumName(Networks[i].BssType));
					lvClientNetworks.SetItemText(i, 3, IntToStr((int)Networks[i].NumberOfBssids));
					lvClientNetworks.SetItemText(i, 4, BoolToStr(Networks[i].NetworkConnectable));

					Str.Format(_T("0x%.8X"), Networks[i].NotConnectableReason);
					lvClientNetworks.SetItemText(i, 5, Str);
					
					Str = _T("");
					for (wclWiFiPhys::iterator j = Networks[i].PhyTypes.begin(); j != Networks[i].PhyTypes.end(); j++)
						Str += GetEnumName(*j) + _T(" ");
					lvClientNetworks.SetItemText(i, 6, Str);

					lvClientNetworks.SetItemText(i, 7, BoolToStr(Networks[i].MorePhyTypes));
					lvClientNetworks.SetItemText(i, 8, IntToStr((int)Networks[i].SignalQuality));
					lvClientNetworks.SetItemText(i, 9, BoolToStr(Networks[i].SecurityEnabled));
					lvClientNetworks.SetItemText(i, 10, GetEnumName(Networks[i].DefaultAuthAlgorithm));
					lvClientNetworks.SetItemText(i, 11, GetEnumName(Networks[i].DefaultCipherAlgorithm));
					Str = _T("");
					if (Networks[i].Flags.find(nfConnected) != Networks[i].Flags.end())
						Str += _T("nfConnected ");
					if (Networks[i].Flags.find(nfHasProfile) != Networks[i].Flags.end())
						Str += _T("nfHasProfile ");
					lvClientNetworks.SetItemText(i, 12, Str);
				}
			}
		}
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientScan()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString s;
		edClientScanSsid.GetWindowText(s);
		if (ShowResult(wclWiFiClient.Scan(Id, tstring(s.GetBuffer()))))
			AfxMessageBox(_T("Scan started. Use EventsDemo to see scan events."));
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientEnumBss()
{
	ClientClearBss();
	
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Ssid = _T("");
		
		if (cbClientUseSsid.GetCheck())
		{
			POSITION Pos = lvClientNetworks.GetFirstSelectedItemPosition();
			if (Pos == NULL)
			{
				AfxMessageBox(_T("Select network"));
				return;
			}
			
			Ssid = lvClientNetworks.GetItemText(lvClientNetworks.GetNextSelectedItem(Pos), 1);
		}
		
		wclWiFiBssType BssType = (wclWiFiBssType)cbClientBssType.GetCurSel();
		wclWiFiBssArray BssList;
		if (ShowResult(wclWiFiClient.EnumBss(Id, tstring(Ssid.GetBuffer()), BssType, cbClientSecurityEnabled.GetCheck() != FALSE, BssList)))
		{
			if (BssList.size() > 0)
			{
				for (unsigned int i = 0; i < BssList.size(); i++)
				{
					lvClientBss.InsertItem(i, BssList[i].Ssid.c_str());
					lvClientBss.SetItemText(i, 1, IntToStr((int)BssList[i].PhyId));
					lvClientBss.SetItemText(i, 2, BssList[i].Mac.c_str());
					lvClientBss.SetItemText(i, 3, GetEnumName(BssList[i].BssType));
					lvClientBss.SetItemText(i, 4, GetEnumName(BssList[i].PhyType));
					lvClientBss.SetItemText(i, 5, IntToStr((int)BssList[i].Rssi));
					lvClientBss.SetItemText(i, 6, IntToStr((int)BssList[i].LinkQuality));
					lvClientBss.SetItemText(i, 7, BoolToStr(BssList[i].InRegDomain));
					lvClientBss.SetItemText(i, 8, IntToStr((int)BssList[i].BeaconPeriod));
					lvClientBss.SetItemText(i, 9, IntToStr((long long)BssList[i].Timestamp));
					lvClientBss.SetItemText(i, 10, IntToStr((long long)BssList[i].HostTimestamp));
					lvClientBss.SetItemText(i, 11, DecodeFlags(BssList[i].Capability));
					lvClientBss.SetItemText(i, 12, IntToStr((int)BssList[i].ChCenterFrequency));
					
					CString s = _T("");
					CString a;
					if (BssList[i].IeRaw.size() > 0)
					{
						for (size_t x = 0; x < BssList[i].IeRaw.size(); x++)
						{
							a.Format(_T("%.2X"), BssList[i].IeRaw[x]);
							s = s + a;
						}
					}
					lvClientBss.SetItemText(i, 13, s);
				}
			}
		}
	}
}

void CWiFiClientDlg::ClientClearBss()
{
	lvClientBss.DeleteAllItems();
}

void CWiFiClientDlg::OnBnClickedButtonClientDisconnect()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
		ShowResult(wclWiFiClient.Disconnect(Id));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetShowDeniedNetworks()
{
	bool Enabled;
	if (ShowResult(wclWiFiClient.AcmGetShowDeniedNetworks(Enabled)))
	{
		if (Enabled)
			AfxMessageBox(_T("Show denied networks"));
		else
			AfxMessageBox(_T("Do not show denied networks"));
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientSetShowDeniedNetworks()
{
	int Res = AfxMessageBox(_T("Show denied networks?"), MB_YESNOCANCEL | MB_ICONQUESTION);
	if (Res == IDCANCEL)
		return;
	
	ShowResult(wclWiFiClient.AcmSetShowDeniedNetworks(Res == IDYES));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetPowerSetting()
{
	wclWiFiPowerSetting Setting;
	if (ShowResult(wclWiFiClient.AcmGetPowerSetting(Setting)))
		AfxMessageBox(GetEnumName(Setting));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetOnlyUseGpProfiles()
{
	bool Enabled;
	if (ShowResult(wclWiFiClient.AcmGetOnlyUseGpProfiles(Enabled)))
	{
		if (Enabled)
			AfxMessageBox(_T("Only use GP profiles ENABLED"));
		else
			AfxMessageBox(_T("Only use GP profiles DISABLED"));
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientGetAllowExplicitCreds()
{
	bool Enabled;
	if (ShowResult(wclWiFiClient.AcmGetAllowExplicitCreds(Enabled)))
	{
		if (Enabled)
			AfxMessageBox(_T("Explicit creds ALLOWED"));
		else
			AfxMessageBox(_T("Explicit creds NOT ALLOWED"));
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientSetAllowExplicitCreds()
{
	int Res = AfxMessageBox(_T("Allow explicit creds?"), MB_YESNOCANCEL | MB_ICONQUESTION);
	if (Res == IDCANCEL)
		return;
	
	ShowResult(wclWiFiClient.AcmSetAllowExplicitCreds(Res == IDYES));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetBlockPeriod()
{
	unsigned long Period;
	if (ShowResult(wclWiFiClient.AcmGetBlockPeriod(Period)))
		AfxMessageBox(_T("Block period: ") + IntToStr((int)Period));
}

void CWiFiClientDlg::OnBnClickedButtonClientSetBlockPeriod()
{
	ShowResult(wclWiFiClient.AcmSetBlockPeriod(1000000));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetVirtualStationExtensibility()
{
	bool Allowed;
	if (ShowResult(wclWiFiClient.AcmGetVirtualStationExtensibility(Allowed)))
	{
		if (Allowed)
			AfxMessageBox(_T("Virtual station extensibility ALLOWED"));
		else
			AfxMessageBox(_T("Virtual station extensibility NOT ALLOWED"));
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientSetVirtualStationExtensibility()
{
	int Res = AfxMessageBox(_T("Allow wirtual station extensibility?"),
		MB_YESNOCANCEL | MB_ICONQUESTION);
	if (Res == IDCANCEL)
		return;

	ShowResult(wclWiFiClient.AcmSetVirtualStationExtensibility(Res == IDYES));
}

void CWiFiClientDlg::OnBnClickedButtonClientGetState()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			wclWiFiInterfaceState State;
			if (ShowResult(Iface->GetState(State)))
				AfxMessageBox(_T("Interface state: ") + GetEnumName(State));
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::ClientClearProfiles()
{
	lvClientProfiles.DeleteAllItems();
}

void CWiFiClientDlg::OnBnClickedButtonClientEnumProfiles()
{
	ClientEnumProfiles();
}

void CWiFiClientDlg::OnBnClickedButtonClientDeleteProfile()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Profile;
		if (ClientGetProfile(Profile))
		{
			if (ShowResult(wclWiFiProfilesManager.DeleteProfile(Id, tstring(Profile.GetBuffer()))))
				ClientEnumProfiles();
		}
	}
}

void CWiFiClientDlg::ClientEnumProfiles()
{
	ClientClearProfiles();
	
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		wclWiFiProfiles Profiles;
		if (ShowResult(wclWiFiProfilesManager.GetProfileList(Id, Profiles)))
		{
			if (Profiles.size() > 0)
			{
				for (unsigned int i = 0; i < Profiles.size(); i++)
				{
					lvClientProfiles.InsertItem(i, Profiles[i].Name.c_str());
					CString Str = _T("");
					if (Profiles[i].Flags.find(pfGroupPolicy) != Profiles[i].Flags.end())
						Str += _T("pfGroupPolicy ");
					if (Profiles[i].Flags.find(pfUser) != Profiles[i].Flags.end())
						Str += _T("pfUser ");
					if (Profiles[i].Flags.find(pfGetPlaintextKey) != Profiles[i].Flags.end())
						Str += _T("pfGetPlaintextKey ");
					lvClientProfiles.SetItemText(i, 1, Str);
				}
			}
		}
	}
}

bool CWiFiClientDlg::ClientGetProfile(CString& Name) const
{
	POSITION Pos = lvClientProfiles.GetFirstSelectedItemPosition();
	if (Pos == NULL)
	{
		AfxMessageBox(_T("Select profile"));
		return false;
	}
	
	Name = lvClientProfiles.GetItemText(lvClientInterfaces.GetNextSelectedItem(Pos), 0);
	return true;
}


void CWiFiClientDlg::OnBnClickedButtonClientRenameProfile()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Profile;
		if (ClientGetProfile(Profile))
		{
			CString s;
			edClientNewProfileName.GetWindowText(s);
			if (ShowResult(wclWiFiProfilesManager.RenameProfile(Id, tstring(Profile), tstring(s))))
				ClientEnumProfiles();
		}
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientGetProfileXml()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Profile;
		if (ClientGetProfile(Profile))
		{
			wclWiFiProfileFlags Flags;
			if (cbPlainText.GetCheck())
				Flags.insert(pfGetPlaintextKey);
			tstring Xml;
			if (ShowResult(wclWiFiProfilesManager.GetProfile(Id, tstring(Profile), Flags, Xml)))
			{
				CString strFilter = _T("XML Files (*.xml)|*.xml|"); 
				Profile = Profile + _T(".xml");

				CFileDialog FileDlg(FALSE, CString(".xml"), Profile, 0, strFilter); 
				
				if (FileDlg.DoModal() == IDOK)
				{
					CStdioFile file;
					if (file.Open(FileDlg.GetPathName(), CFile::modeCreate | CFile::modeWrite))
					{
						CString XmlStr(Xml.c_str());
						file.WriteString(XmlStr);
					}
				}
			}
		}
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientSetProfileExml()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString strFilter = _T("XML Files (*.xml)|*.xml|"); 
		
		CFileDialog FileDlg(TRUE, CString(".xml"), NULL, 0, strFilter);
		
		if (FileDlg.DoModal() == IDOK)
		{
			CStdioFile file;
			if (file.Open(FileDlg.GetPathName(), CFile::modeRead | CFile::typeText))
			{
				CString Xml;
				CString str;
				while (file.ReadString(str))
					Xml = Xml + str;
				
				ShowResult(wclWiFiProfilesManager.SetProfile(Id, wclWiFiProfileFlags(), tstring(Xml), true));
			}
		}
	}
}

void CWiFiClientDlg::OnBnClickedButtonClientConnect()
{
	switch (cbClientConnect.GetCurSel())
	{
		case 0:
			ClientConnectUsingNetworkProfile();
			break;
		case 1:
			ClientConnectUsingSelectedProfile();
			break;
		case 2:
			ClientConnectUsingSelectedNetwork();
			break;
		case 3:
			ClientConenctUsingTemporaryProfile();
			break;
	}
}

void CWiFiClientDlg::ClientConenctUsingTemporaryProfile()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		POSITION Pos = lvClientNetworks.GetFirstSelectedItemPosition();
		if (Pos == NULL)
			AfxMessageBox(_T("Select network"));
		else
		{
			int Item = lvClientNetworks.GetNextSelectedItem(Pos);
			CString Ssid = lvClientNetworks.GetItemText(Item, 1);

			Pos = lvClientBss.GetFirstSelectedItemPosition();
			if (cbUseBssMac.GetCheck() && Pos == NULL)
				AfxMessageBox(_T("Select Bss"));
			else
			{
				CString Mac = _T("");
				if (cbUseBssMac.GetCheck())
				{
					Item = lvClientBss.GetNextSelectedItem(Pos);
					Mac = lvClientBss.GetItemText(Item, 2);
				}

				CHAR Utf8[255];
				ZeroMemory(Utf8, 255);
				wclUTF8Encode(tstring(Ssid), Utf8, 255);
				CString Hex = _T("");
				for (size_t i = 0; i < strlen(Utf8); i++)
					Hex = Hex + IntToHex(Utf8[i]);
				
				CString Xml(_T("<?xml version=\"1.0\" encoding=\"US-ASCII\"?> \
							   <WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"> \
							   <name>%s</name> \
							   <SSIDConfig> \
							   <SSID> \
							   <hex>%s</hex> \
							   <name>%s</name> \
							   </SSID> \
							   </SSIDConfig> \
							   <connectionType>ESS</connectionType> \
							   <connectionMode>manual</connectionMode> \
							   <autoSwitch>false</autoSwitch> \
							   <MSM> \
							   <security> \
							   <authEncryption> \
							   <authentication>%s</authentication> \
							   <encryption>%s</encryption> \
							   <useOneX>false</useOneX> \
							   </authEncryption> \
							   <sharedKey> \
							   <keyType>passPhrase</keyType> \
							   <protected>false</protected> \
							   <keyMaterial>%s</keyMaterial> \
							   </sharedKey> \
							   </security> \
							   </MSM> \
							   </WLANProfile>"));
				
				CString Key;
				edClientPasskey.GetWindowText(Key);
				CString AuthAlg;
				if (lvClientNetworks.GetItemText(Item, 10) == _T("auOpen"))
					AuthAlg = _T("open");
				else
				{
					if (lvClientNetworks.GetItemText(Item, 10) == _T("auSharedKey"))
						AuthAlg = _T("shared");
					else
					{
						if (lvClientNetworks.GetItemText(Item, 10) == _T("auWpa"))
							AuthAlg = _T("WPA");
						else
						{
							if (lvClientNetworks.GetItemText(Item, 10) == _T("auWpaPsk"))
								AuthAlg = _T("WPAPSK");
							else
							{
								if (lvClientNetworks.GetItemText(Item, 10) == _T("auRsna"))
									AuthAlg = _T("WPA2");
								else
								{
									if (lvClientNetworks.GetItemText(Item, 10) == _T("auWpa3"))
										AuthAlg = _T("WPA3ENT192");
									else
									{
										if (lvClientNetworks.GetItemText(Item, 10) == _T("auWpa3Sae"))
											AuthAlg = _T("WPA3SAE");
										else
										{
											if (lvClientNetworks.GetItemText(Item, 10) == _T("auWpa3Ent"))
												AuthAlg = _T("WPA3ENT");
											else
											{
												if (lvClientNetworks.GetItemText(Item, 10) == _T("auOwe"))
													AuthAlg = _T("OWE");
												else
													AuthAlg = _T("WPA2PSK");
											}
										}
									}
								}
							}
						}
					}
				}
				CString CipherAlg;
				if (lvClientNetworks.GetItemText(Item, 11) == _T("caNone"))
					CipherAlg = _T("none");
				else
				{
					if (lvClientNetworks.GetItemText(Item, 11) == _T("caTkip"))
						CipherAlg = _T("TKIP");
					else
					{
						if (lvClientNetworks.GetItemText(Item, 11) == _T("caWep40") ||
							lvClientNetworks.GetItemText(Item, 11) == _T("caWep104") ||
							lvClientNetworks.GetItemText(Item, 11) == _T("caWep"))
						{
							CipherAlg = _T("WEP");
						}
						else
						{
							if (lvClientNetworks.GetItemText(Item, 11) == _T("caGcmp256"))
								CipherAlg = _T("GCMP256");
							else
								CipherAlg = _T("AES");
						}
					}
				}
				CString s;
#pragma warning(disable:4840)
				s.Format(Xml, Ssid, Hex, Ssid, AuthAlg, CipherAlg, Key);
#pragma warning(default:4840)
				
				ShowResult(wclWiFiClient.Connect(Id, cmTemporaryProfile, tstring(s), tstring(Ssid),
					ClientGetNetworkBss(), wclWiFiConnectFlags(), tstring(Mac)));
			}
		}
	}
}

void CWiFiClientDlg::ClientConnectUsingNetworkProfile()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		POSITION Pos = lvClientNetworks.GetFirstSelectedItemPosition();
		if (Pos == NULL)
			AfxMessageBox(_T("Select network"));
		else
		{
			int Item = lvClientNetworks.GetNextSelectedItem(Pos);
			CString Ssid = lvClientNetworks.GetItemText(Item, 0);

			Pos = lvClientBss.GetFirstSelectedItemPosition();
			if (cbUseBssMac.GetCheck() && Pos == NULL)
				AfxMessageBox(_T("Select Bss"));
			else
			{
				CString Mac = _T("");
				if (cbUseBssMac.GetCheck())
				{
					Item = lvClientBss.GetNextSelectedItem(Pos);
					Mac = lvClientBss.GetItemText(Item, 2);
				}
				
				ShowResult(wclWiFiClient.Connect(Id, cmProfile, tstring(Ssid), tstring(_T("")),
					ClientGetNetworkBss(), wclWiFiConnectFlags(), tstring(Mac)));
			}
		}
	}
}

void CWiFiClientDlg::ClientConnectUsingSelectedNetwork()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		POSITION Pos = lvClientNetworks.GetFirstSelectedItemPosition();
		if (Pos == NULL)
			AfxMessageBox(_T("Select network"));
		else
		{
			int Item = lvClientNetworks.GetNextSelectedItem(Pos);
			CString Ssid = lvClientNetworks.GetItemText(Item, 1);

			Pos = lvClientBss.GetFirstSelectedItemPosition();
			if (cbUseBssMac.GetCheck() && Pos == NULL)
				AfxMessageBox(_T("Select Bss"));
			else
			{
				CString Mac = _T("");
				if (cbUseBssMac.GetCheck())
				{
					Item = lvClientBss.GetNextSelectedItem(Pos);
					Mac = lvClientBss.GetItemText(Item, 2);
				}
				
				wclWiFiConnectionMode Mode;
				if (cbClientDiscoverSecure.GetCheck())
					Mode = cmDiscoverySecure;
				else
					Mode = cmDiscoveryUnsecure;
				
				ShowResult(wclWiFiClient.Connect(Id, Mode, tstring(_T("")), tstring(Ssid),
					ClientGetNetworkBss(), wclWiFiConnectFlags(), tstring(Mac)));
			}
		}
	}
}

void CWiFiClientDlg::ClientConnectUsingSelectedProfile()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		POSITION Pos = lvClientProfiles.GetFirstSelectedItemPosition();
		if (Pos == NULL)
			AfxMessageBox(_T("Select profile"));
		else
		{
			int Item = lvClientProfiles.GetNextSelectedItem(Pos);
			CString Prof = lvClientProfiles.GetItemText(Item, 0);

			Pos = lvClientBss.GetFirstSelectedItemPosition();
			if (cbUseBssMac.GetCheck() && Pos == NULL)
				AfxMessageBox(_T("Select Bss"));
			else
			{
				CString Mac = _T("");
				if (cbUseBssMac.GetCheck())
				{
					Item = lvClientBss.GetNextSelectedItem(Pos);
					Mac = lvClientBss.GetItemText(Item, 2);
				}
				
				ShowResult(wclWiFiClient.Connect(Id, cmProfile, tstring(Prof), tstring(_T("")),
					ClientGetNetworkBss(), wclWiFiConnectFlags(), tstring(Mac)));
			}
		}
	}
}

wclWiFiBssType CWiFiClientDlg::ClientGetNetworkBss() const
{
	POSITION Pos = lvClientNetworks.GetFirstSelectedItemPosition();
	if (Pos == NULL)
		return (wclWiFiBssType)cbClientBssType.GetCurSel();

	int Item = lvClientProfiles.GetNextSelectedItem(Pos);
	CString Bss = lvClientProfiles.GetItemText(Item, 1);

	if (Bss = _T("bssInfrastructure"))
		return bssInfrastructure;

	if (Bss = _T("bssIndependent"))
		return bssIndependent;
	
	throw new wclException("Inavlid BSS type");
}

void CWiFiClientDlg::OnBnClickedButtonTurnOn()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
		ClientSwitchState(Id, false);
}

void CWiFiClientDlg::OnBnClickedButtonTurnOff()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
		ClientSwitchState(Id, true);
}

void CWiFiClientDlg::ClientSwitchState(const GUID& Id, const bool Off)
{
	CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
	if (ShowResult(Iface->Open()))
	{
		if (Off)
			ShowResult(Iface->TurnOff());
		else
			ShowResult(Iface->TurnOn());
		Iface->Close();
	}
	delete Iface;
}

void CWiFiClientDlg::OnBnClickedButtonCurrentIp()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			bool Static;
			tstring Dns1;
			tstring Dns2;
			tstring Address;
			tstring Gateway;
			tstring Mask;
			int Res = Iface->GetCurrentIp(Static, Address, Mask, Gateway, Dns1, Dns2);
			if (ShowResult(Res))
			{
				CString Msg;
				if (Static)
					Msg = _T("Static IP\n");
				else
					Msg = _T("DHCP\n");
				Msg = Msg + _T("Address: ") + Address.c_str() + 
					_T("\nSubnet mask: ") + Mask.c_str() +
					_T("\nDefault gateway: ") + Gateway.c_str() +
					_T("\nName server 1: ") + Dns1.c_str() +
					_T("\nName server 2: ") + Dns2.c_str();
				AfxMessageBox(Msg);
			}
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::OnBnClickedButtonGetIpSettings()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			bool Static;
			tstring Dns1;
			tstring Dns2;
			tstring Address;
			tstring Gateway;
			tstring Mask;
			int Res = Iface->GetIpSettings(Static, Address, Mask, Gateway, Dns1, Dns2);
			if (ShowResult(Res))
			{
				CString Msg;
				if (Static)
					Msg = _T("Static IP\n");
				else
					Msg = _T("DHCP\n");
				Msg = Msg + _T("Address: ") + Address.c_str() + 
					_T("\nSubnet mask: ") + Mask.c_str() +
					_T("\nDefault gateway: ") + Gateway.c_str() +
					_T("\nName server 1: ") + Dns1.c_str() +
					_T("\nName server 2: ") + Dns2.c_str();
				AfxMessageBox(Msg);
			}
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::OnBnClickedButtonEnableDhcp()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			ShowResult(Iface->EnableDhcp());
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::OnBnClickedButtonEnableStaticIp()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			ShowResult(Iface->SetStaticIp(_T("192.168.1.210"), _T("255.255.255.0"),
				_T("192.168.1.1"), _T("192.168.1.1"), _T("")));
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::OnBnClickedButtonSjowEditUi()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Profile;
		if (ClientGetProfile(Profile))
		{
			ShowResult(wclWiFiProfilesManager.ShowUIEdit(Id,
				tstring(Profile.GetBuffer())));
		}
	}
}

void CWiFiClientDlg::OnBnClickedButtonSetUserData()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CString Profile;
		if (ClientGetProfile(Profile))
		{
			CString strFilter = _T("XML Files (*.xml)|*.xml|");
			CFileDialog FileDlg(TRUE, CString(".xml"), NULL, 0, strFilter);

			if (FileDlg.DoModal() == IDOK)
			{
				CStdioFile file;
				if (file.Open(FileDlg.GetPathName(), CFile::modeRead | CFile::typeText))
				{
					CString Xml;
					CString str;
					while (file.ReadString(str))
						Xml = Xml + str;
					
					ShowResult(wclWiFiProfilesManager.SetProfileEapXmlUserData(Id,
						tstring(Profile.GetBuffer()), false, tstring(Xml.GetBuffer())));
				}
			}
		}
	}
}

void CWiFiClientDlg::OnBnClickedButton1()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			bool State;
			if (ShowResult(Iface->GetSecondarySta(State)))
				AfxMessageBox(_T("Dual-STA state: ") + BoolToStr(State));
			Iface->Close();
		}
		delete Iface;
	}
}

void CWiFiClientDlg::ClientSetDualState(const bool Enable)
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			ShowResult(Iface->SetSecondarySta(Enable));
			Iface->Close();
		}
		delete Iface;
	}
}
void CWiFiClientDlg::OnBnClickedButton3()
{
	ClientSetDualState(false);
}

void CWiFiClientDlg::OnBnClickedButton2()
{
	ClientSetDualState(true);
}

void CWiFiClientDlg::OnBnClickedButton4()
{
	GUID Id;
	if (ClientGetInterfaceId(Id))
	{
		CwclWiFiInterface* Iface = new CwclWiFiInterface(Id);
		if (ShowResult(Iface->Open()))
		{
			wclWiFiInterfaces Ifaces;
			if (ShowResult(Iface->EnumInterfaces(Ifaces)))
			{
				ClientClearInterfaces();
				if (Ifaces.size() > 0)
				{
					for (unsigned int i = 0; i < Ifaces.size(); i++)
					{
						lvClientInterfaces.InsertItem(i, GuidToString(Ifaces[i].Id));
						lvClientInterfaces.SetItemText(i, 1, Ifaces[i].Description.c_str());
						lvClientInterfaces.SetItemText(i, 2, BoolToStr(Ifaces[i].Primary));
					}
				}
			}
			Iface->Close();
		}
		delete Iface;
	}
}
