/* WrapiTest.cpp : Defines the entry point for a console application using WRAPI.dll.
 *
 * WrapiTest.cpp demonstrates the use of the most common WRAPI functions. It also deals with
 * memory allocation and deallocation. For detailed implementation of these functions, check 
 * out the WRAPI Source code.
 */

#include "stdafx.h"
#include "WrapiExports.h"

int main(int argc, char* argv[])
{

	WRAPI_NDIS_DEVICE		*pDeviceList = NULL;
	HRESULT				hRes;
	
	long				lItems = 0;
	long				lNumItems = 0;
	UCHAR				Ssid[32] = {0};
	
	AP_DATA				*pAP_list = NULL;
	CHAR				*pSSId = "UCSD";
	
	ULONG				lSSIdLength = strlen(pSSId);
	ULONG				lRTSThresh = 0;
	
	MAC_ADDR			addr = {0};
	DOT_11_STATS			DOT11Stats;
	
	/* Get the list of Devices in the System */
	hRes = WRAPIEnumerateDevices(&pDeviceList, &lItems);

	/* Print the list of devices obtained */
	for ( int i = 0; i < lItems; i++)
	{
		printf("%ws\n  - %ws\n", pDeviceList[i].pDeviceDescription, pDeviceList[i].pDeviceName);
	}

	/* Open the device at pDeviceList[1] -- pDeviceList[0] happens to be the 	 * Ethernet Interface */
	hRes = WRAPIOpenNdisDevice(pDeviceList[1].pDeviceName);

	/* Get the SSID Of the Network */
	hRes = WRAPIGetSSId(Ssid);

	/* Set the network SSID to "UCSD" */
	hRes = WRAPISetSSId((UCHAR*)pSSId, lSSIdLength);
	
	/* Get the value of RTS Threshold */
	hRes = WRAPIGetRTSThreshold(&lRTSThresh);
	
	/* Get the MAC address of the AP to which this station is associated */
	hRes = WRAPIGetAssociatedAP(addr);
	
	/* Get Packet-level statistics from the 802.11 interface */
	hRes = WRAPIGetPacketStats(&DOT11Stats);

	/* Get a list of all APs within range of this station in a list of AP_DATA
	 * structures */
	hRes = WRAPIGetAPList(&pAP_list, &lNumItems);


	/* Print out the list of all APs and their signal strengths */

	printf("MAC Address \t\tSignal Strength (dBm) \n");

	for ( i = 0; i < lNumItems; i++ )
	{
		for ( int j = 0; j < 6; j++)
		{
			printf("%02x", pAP_list[i].mac_addr[j]);
		}

		printf("\t\t%d\n", pAP_list[i].Rssi);
	}


	/* Free all the memory that gets allocated inside the WRAPI dll */

	free(pAP_list);

	for ( i = 0; i < lItems; i++ )
	{
		free(pDeviceList[i].pDeviceName);
		free(pDeviceList[i].pDeviceDescription);
	}

	free(pDeviceList);

	return 0;
}
