//// Defined to disable deprecated functions warning in Visual C++ 2003
//#define _CRT_SECURE_NO_DEPRECATE 1

#include <windows.h>
#include <stdio.h>

typedef int			RADIOHANDLE;
typedef void		(__stdcall* RADIOHANDLERCALLBACK)(void* pTarget, void* pBuffer, DWORD dwSize, DWORD dwSamplingRate);

// G33WSB API function type declarations
typedef int		(__stdcall *FNCOpenRadioDevice)(int iDeviceNum);
typedef BOOL	(__stdcall *FNCCloseRadioDevice)(RADIOHANDLE hRadio);
typedef BOOL	(__stdcall *FNCSetAtten)(RADIOHANDLE hRadio, BOOL fAtten);
typedef BOOL	(__stdcall *FNCSetPower)(RADIOHANDLE hRadio, BOOL fPower);
typedef BOOL	(__stdcall *FNCInitializeDemodulator)(RADIOHANDLE hRadio, char* pszCalFilePath);
typedef int		(__stdcall *FNCSetFreqPipelined)(RADIOHANDLE hRadio, DWORD dwFreq, BOOL IsFirst, LONG Settling);
typedef int		(__stdcall *FNCGetSSPipelined)(RADIOHANDLE hRadio, int *SS, BYTE *RawSS, int *RSSI, int *AGC);
typedef BOOL	(__stdcall *FNCSetupStreams)(RADIOHANDLE hRadio, RADIOHANDLERCALLBACK IFHandler, void* IFTarget, RADIOHANDLERCALLBACK AudioHandler, void* AudioTarget);

// Formula for the frequencies to be scanned
#define ScanFreqsNum	99
#define ScanFreq(i)		((DWORD)(136000000+(i)*(375000)))

int main(int argc, char* argv[])
{

	// load the G3 API library
	HMODULE dll = LoadLibrary("wrg39wsbapi.dll");
	if (!dll) 
	{
		puts("WRG39WSB.DLL not found !"); 
		return 0;
	}

	// link G3 API functions
	FNCOpenRadioDevice			OpenRadioDevice			= (FNCOpenRadioDevice)GetProcAddress(dll,"OpenRadioDevice");
	FNCCloseRadioDevice			CloseRadioDevice		= (FNCCloseRadioDevice)GetProcAddress(dll,"CloseRadioDevice");
	FNCSetAtten					SetAtten				= (FNCSetAtten)GetProcAddress(dll,"SetAtten");
	FNCSetPower					SetPower				= (FNCSetPower)GetProcAddress(dll,"SetPower");
	FNCInitializeDemodulator	InitializeDemodulator	= (FNCInitializeDemodulator)GetProcAddress(dll,"InitializeDemodulator");
	FNCSetFreqPipelined			SetFreqPipelined		= (FNCSetFreqPipelined)GetProcAddress(dll,"SetFreqPipelined");
	FNCGetSSPipelined			GetSSPipelined			= (FNCGetSSPipelined)GetProcAddress(dll,"GetSSPipelined");
	FNCSetupStreams				SetupStreams			= (FNCSetupStreams)GetProcAddress(dll,"SetupStreams");

	// open the first available radio
	RADIOHANDLE hRadio = OpenRadioDevice(0);
	if (!hRadio) 
	{
		puts("No G39WSB device could be opened !");
		FreeLibrary(dll); 
		return 0;
	}

	// Initialize device and set Power, Attenuator
	if (SetPower(hRadio,TRUE)) 
		puts("The device is turned ON");
	else 
		puts("The device failed to turn ON");

	if (SetAtten(hRadio,FALSE)) 
		puts("The attenuator is OFF");
	else 
		puts("The attenuator failed to turn OFF");

	if (InitializeDemodulator(hRadio, NULL)) 
		puts("The device is initialized");
	else 
		puts("The device failed to initialization");


	// let's start the pipelined scanning by passing the first frequency
	if (SetFreqPipelined(hRadio, ScanFreq(0), TRUE, 20)) 
		puts("Error while pipelining the first frequency..");
	else 
	{
		size_t i;

		puts("First frequency successfully pipelined..");
		// now it does all steps that can be done in a continous loop
		for (i = 1; i <= ScanFreqsNum; i++) 
		{
			// pipelines the next frequency
			if (SetFreqPipelined(hRadio, ScanFreq(i),FALSE,3)) 
				printf("Error while pipelining %u..\n",ScanFreq(i));
			else 
			{
				int SS,RSSI,AGC,Res;
				BYTE RawSS;

				Res=GetSSPipelined(hRadio,&SS,&RawSS,&RSSI,&AGC);
				if (Res) 
					printf("Error while reading SS for %u..\n",ScanFreq(i-1));
				else 
					printf("%u - SS=%d   RawSS=%u   AGC=%d   RSSI=%d\n",ScanFreq(i-1),SS,RawSS,AGC,RSSI);
			}
		}
		// the first step requires a dummy frequency pipelining
		if (SetFreqPipelined(hRadio,ScanFreq(ScanFreqsNum),FALSE,3)) 
			printf("Error while re-pipelining %u..\n",ScanFreq(ScanFreqsNum));
		else 
		{
			int SS,RSSI,AGC,Res;
			BYTE RawSS;

			Res=GetSSPipelined(hRadio,&SS,&RawSS,&RSSI,&AGC);
			if (Res) 
				printf("Error while reading SS for %u..\n",ScanFreq(ScanFreqsNum));
			else 
				printf("%u - SS=%d   RawSS=%u   AGC=%d   RSSI=%d\n",ScanFreq(ScanFreqsNum),SS,RawSS,AGC,RSSI);
		}
	}

	// close the device handle
	if (CloseRadioDevice(hRadio)) 
		puts("The device is closed properly");
	else 
		puts("The device failed to close");

	// free the G3 API library
	FreeLibrary(dll);

	puts("Press <Enter>");
	getchar();

	return 0;
}
