/******************************************************************************************/
// includes
#include <stdio.h>
#include <stdlib.h>
#include "../Src/ATDemo.h"
#include "../Src/Support/strfunc.h"


CAtBoardManager *BoardManager;
CATBoardModDVBS2 ATBoardModDVBS2;

static void PrintDeviceInfo(CATBoardPlayRec *pATBoardPlayRec);

void SysConsoleHandlersCallBack()
{
	Message(E_VERBOSE_1, "Closing down.......");
	RecordFileBreak((CATBoardPlayRec*)&ATBoardModDVBS2);
}

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

	if(argc <= 1)
	{
		printf("Incorrect arguments. \nUse the filename as a argument\n");
	}

	SetProgramName("ATBoard2900Rec");
	SetVerboseLevel(E_VERBOSE_ALL);

	SysSetupConsoleHandlers();

	// Instantiate the board manager
	BoardManager = CAtBoardManager::Instance ();

	// Get a device list
	CAtDeviceList DeviceList;
	BoardManager->GetDeviceList(DeviceList);


	SBoardOpenParams BoardOpenParams;

	// Set the default board index
	BoardOpenParams.m_BoardPos = 0;

	// Use the first board found. If none are found, use the demo board.
	if(DeviceList.GetFreeSize() == 0)
	{
		printf("No (free) device(s) found. Using demo board.\n");

		// set demo board open parameters
		BoardOpenParams.m_BoardPos = -1;
		BoardOpenParams.m_pBoardName = NULL;
	}
	else
	{

		//find the first board that can be used for this application

		BOOL bRet = DeviceList.GetFirstBoard(&BoardOpenParams);
		const ATDEVICEINFO *pDevInfo = DeviceList.GetDeviceInfo(&BoardOpenParams);
		
		while(bRet && pDevInfo->DeviceType != AT2900)
		{
			bRet = DeviceList.GetNextBoard(&BoardOpenParams);
			pDevInfo = DeviceList.GetDeviceInfo(&BoardOpenParams);
		}

		if (!bRet || !DeviceList.IsBoardFree(&BoardOpenParams))
		{
			//required board type is not present or already in use.
			printf("Requested device could not be found or is in use. Using demo board.\n");
			// set demo board open parameters
			BoardOpenParams.m_BoardPos = -1;
			BoardOpenParams.m_pBoardName = NULL;
		}

	}


	//////////////////////////////////////////////////////////////////////////
	//Open the selected device
	//////////////////////////////////////////////////////////////////////////
	const ATDEVICEINFO *pDevInfo = DeviceList.GetDeviceInfo(&BoardOpenParams);
	
	printf("Open and initializing board....\n");

	ATBoardModDVBS2.BoardOpen(BoardOpenParams);
	Message(E_VERBOSE_2, "Open and initializing board done");
	PrintDeviceInfo(&ATBoardModDVBS2);


	/************************************************************************/
	/* The Modulator settings                                               */
	/************************************************************************/

	SModDVBS2Opt ModDVBS2Options =
	{
		TRUE,						///< selects if RF output is selected or IF output. 1: RF is selected, 0: IF is selected
		0UL,						///< (absolute) IF frequency in Hz for. (ranges: 49-51MHz, 99-101MHz)
		1500000000UL,				///< (absolute) RF frequency in Hz for. (range: 950-2150MHz)
		-350,						///< (absolute) output level for the modulators in 0.1dBm units. (range: -45 to -10dBm in 0.5dBm steps)
		QPSK,						///< modulation type
		FEC_1_2,					///< FEC code rate
		FLT_ALPHA_35,				///< set alpha filter roll off factor
		INVERSION_OFF,				///< spectral inversion selection
		22500000,					///< symbol rate in Symbols per second (1-45MSymbols/s)
		DVBS_MODE_S					///< set dvb mode
	};

	/************************************************************************/
	/* The Record file  settings                                              */
	/************************************************************************/
	SRecOpt ROptions =
	{
		0UL,					
		60UL					///< record a file of 60secs
	};

	if(SetModDVBS2((CATBoardModDVBS2 *)&ATBoardModDVBS2, ModDVBS2Options))
	{
		// Perform the actual record action
		if (!RecordFile(&ATBoardModDVBS2, argv[1], ROptions))
			Message(E_VERBOSE_SILENT, "Record terminated with an error");
	}
	else
	{
		printf("Error while initializing modulator\n");
	}

	ATBoardModDVBS2.BoardClose();
}




// Print some device information 
static void PrintDeviceInfo(CATBoardPlayRec *pATBoardPlayRec)
{
	CATBoard *pTheBoard = pATBoardPlayRec->GetCurBoard();

	char TmpStr[MAX_TMP_STR_LEN];

	// print friendly name
	u32 nLength = ATDEV_INFO_MAX_STR_LEN;
	char * pFriendlyName = new char[nLength];
	pTheBoard->GetFriendlyName(pFriendlyName, nLength);

	sprintf(TmpStr, "Board found: %s", pFriendlyName);
	Message(E_VERBOSE_SILENT, TmpStr);

	if (pFriendlyName)
		delete pFriendlyName;

	// print API version information
	ATVERSIONINFO * pAPIInfo = NULL;
	pAPIInfo = pTheBoard->GetAPIVersion();
	sprintf(TmpStr, "API version %d.%d", pAPIInfo->Mayor, pAPIInfo->Minor);
	Message(E_VERBOSE_2, TmpStr);

	// print driver version information
	ATVERSIONINFO * pDriverInfo = NULL;
	pDriverInfo = pTheBoard->GetDriverVersion();

	if(pDriverInfo  != NULL)
	{
		sprintf(TmpStr, "Driver version %d.%d", pDriverInfo->Mayor, pDriverInfo->Minor);
		Message(E_VERBOSE_2, TmpStr);
	}
}
