//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <iostream>
#include <conio.h>
#include <mvIMPACT_CPP/mvIMPACT_acquire.h>

#ifdef linux
#define PRESS_A_KEY				\
		cout << "Press a key..." << endl;	\
		getchar();
#else
#define PRESS_A_KEY				\
		cout << "Press a key..." << endl;	\
		char ch = getch();
#endif

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
using namespace mvIMPACT::acquire;
using namespace std;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int main( int argc, char* argv[])
//-----------------------------------------------------------------------------
{
	char* pDevSerial = "BF*";

	cout << "\n ++ Start FirmwareUpgrade: " << __DATE__ << "/" << __TIME__ << endl;

	if( argc > 1 )
		pDevSerial = argv[1];

	//
	unsigned int uiDevCount = 0;
	DeviceManager DevMgr;
	if(( uiDevCount = DevMgr.deviceCount()) == 0 )
	{
		cout << "*** Error: No MATRIX VISION device found! Unable to continue!" << endl;
		PRESS_A_KEY
		return 0;
	}

	cout << "Have found " << uiDevCount << " devices on this platform!" << endl;

	for( unsigned i = 0; i < uiDevCount; i++ )
	{
		Device* pDevTmp = DevMgr.getDevice( i );

		cout << " " << i << " Serial: " << pDevTmp->serial.read() << endl;
	}

	cout << "Initialising the device: "<< pDevSerial << ". This might take some time..." << endl;

	// create an interface to the first MATRIX VISION device with the serial number pDevSerial
	Device* pDev = DevMgr.getDeviceBySerial( pDevSerial );
#if 0
	try
	{
		pDev->open();
	}
	catch( ImpactAcquireException& e )
	{
		// this e.g. might happen if the same device is already opened in another process...
		cout << "*** Error: An error occurred while opening the device(error code: " << e.errCode() << "). Press any key to end the application..." << endl;
		PRESS_A_KEY
		return 0;
	}
#endif
	cout << "The firmware of device " << pDev->serial.read() << " is currently " << pDev->firmwareVersion.readS() << "." << endl;
	cout << "It will now be updated. During this time(approx. 30 sec.) the application will not react. Please be patient." << endl;

	int result = pDev->updateFirmware();
	if( result == DMR_FEATURE_NOT_AVAILABLE )
	{
		cout << "This device doesn't support firmware updates." << endl;
	}
	else if( result != DMR_NO_ERROR )
	{
		cout << "An error occurred: " << ImpactAcquireException::getErrorCodeAsString( result ) << ". (please refer to the manual for this error code)." << endl;
	}
	else
	{
		cout << pDev->HWUpdateResult.readS() << endl;
		if( pDev->HWUpdateResult.read() == urUpdateFWOK )
		{
			cout << "Update successful." << endl;
			if( pDev->family.read() == "mvBlueFOX" )
			{
				cout << "Please disconnect and reconnect the device now to activate the new firmware." << endl;
			}
		}
		else
		{
			cout << "Firmware update canceled for device " << pDev->serial.read() << endl;
		}
	}
#if 0
	pDev->close();
#endif
	cout << "Press any key to end the application" << endl;
	PRESS_A_KEY

	return 0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


