// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "pch.h"

#include "..\..\..\..\Source\WiFi\wclWiFi.h"

using namespace std;
using namespace wclCommon;
using namespace wclWiFi;

string GuidToString(const GUID& guid)
{
	char guid_cstr[39];
	snprintf(guid_cstr, sizeof(guid_cstr),
		"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
		guid.Data1, guid.Data2, guid.Data3,
		guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
		guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);

	return string(guid_cstr);
};

string wtoa(const tstring& w)
{
	return string(w.begin(), w.end());
}

int main(int argc, char **argv)
{
	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);

	CwclMessageBroadcaster::SetSyncMethod(skApc);

	system("cls");
	
	cout << "WIFI CLIENT DEMO APPLICATION" << endl;
	cout << "============================" << endl;
	cout << "This demo enumerates WiFi interfaces, networks and BBS" << endl;

	CwclWiFiClient Client;

	int Res = Client.Open();
	if (Res != WCL_E_SUCCESS)
		cout << "Failed to open WiFi Direct client: 0x" << hex << Res << dec << endl;
	else
	{
		wclWiFiInterfaces Ifaces;
		Res = Client.EnumInterfaces(Ifaces);
		if (Res != WCL_E_SUCCESS)
			cout << "Failed to enumerate WiFi interfaces: 0x" << hex << Res << dec << endl;
		else
		{
			for (wclWiFiInterfaces::iterator i = Ifaces.begin(); i != Ifaces.end(); i++)
			{
				wclWiFiInterfaceData IfaceData = (*i);
				GUID Id = IfaceData.Id;

				cout << "Interface " << GuidToString(Id) << " found" << endl;
				cout << "  Description: " << wtoa(IfaceData.Description) << endl;

				cout << "    Try to enumerate available networks" << endl;
				wclWiFiAvailableNetworkFilters Filters;
				wclWiFiAvailableNetworks Networks;
				Filters.clear();
				Res = Client.EnumAvailableNetworks(Id, Filters, Networks);
				if (Res != WCL_E_SUCCESS)
					cout << "      Failed to enumerate available networks: 0x" << hex << Res << dec << endl;
				else
				{
					if (Networks.size() > 0)
					{
						for (wclWiFiAvailableNetworks::iterator n = Networks.begin(); n != Networks.end(); n++)
						{
							wclWiFiAvailableNetwork Network = (*n);
							// Ignore networks that has profile.
							if (Network.ProfileName == _T("") && Network.Ssid != _T(""))
							{
								cout << "      SSID: " << wtoa(Network.Ssid) << endl;
								wclWiFiBssArray BssList;
								Res = Client.EnumBss(Id, Network.Ssid, bssInfrastructure, true, BssList);
								if (Res != WCL_E_SUCCESS)
									cout << "        Failed to enumerate BSS: 0x" << hex << Res << dec << endl;
								else
								{
									for (wclWiFiBssArray::iterator b = BssList.begin(); b != BssList.end(); b++)
									{
										wclWiFiBss Bss = (*b);

										cout << "        BSS: " << wtoa(Bss.Ssid) << endl;
										cout << "          Frequency: " << Bss.ChCenterFrequency << "; MAC: " << wtoa(Bss.Mac) <<
											"; RSSI: " << Bss.Rssi << endl;
									}
								}
							}
						}
					}
				}
			}
		}
		Client.Close();
		system("pause");
	}
}
