// test.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <locale.h>
#include "mxnet1_api.h"

#define RETRY_COUNT 3

int loop( int UserCode, int dNr )
{
	int result;
	long data[16];
	short retry;
	long  version;

	//
	// All remote API functions internally make a few retries 
	// before returning an error.
	//
	// The code calling the remote API may also call the same API functions
	// repeatedly to circumvent the server connect error ( -100 ).
	// Test your program to see if this may be necessary
	//
	for( retry=0 ; ; retry++ )
	{
		// Start Session
		result=rInit_MatrixAPI();
		if( result < 0 )
		{
			// server may be busy so retry to connect
			if( result == -100 && retry < RETRY_COUNT)
				continue;

			printf( "Init_MatrixAPI error %d\n", result);
			return -1;
		}
		break;
	}

	// retry sample
	for( retry=0 ;  ; retry++ )
	{
		version = rGetVersionAPI();	
		if( version < 0 )
		{
			// server may be busy so retry to connect
			if( version == -100 && retry < RETRY_COUNT)
				continue;

		}
		break;
	}
	if( version < 0 )
		printf("GetVersionAPI Version Error\n", version);
	else
		printf("API Version =%d.%d\n", HIWORD(version), LOWORD(version));

	// retry sample
	for( retry=0 ; retry < 3 ; retry++ )
	{
		result  = rDongle_Count(85);	
		if( result < 0 )
		{
			// server may be busy so retry to connect
			if( result == -100 && retry < RETRY_COUNT)
				continue;

		}
		break;
	}
	if( result < 0 )
		printf( "Count Error %d\n", result);
	else
		printf( "Count %d\n", result);

	// no retry
	version = rDongle_Version(dNr, 85);
	if( version > 0 )
		printf("Dongle Version =%d.%d\n", HIWORD(version), LOWORD(version));
	else
		printf("Dongle_Version Error\n", version);


	// no retry
	// Read & Write
	result = rDongle_ReadData( UserCode,data, 5, dNr, 85 );
	if ( result == 5 )
	{
		if( data[0]==0 )
		{
			data[0]=77;
			rDongle_WriteData(UserCode, data, 1, dNr, 85 );
		}
	}
	

	// no retry
	// Attemp to write the encryption keys only when
	// none has been set.
	result = rDongle_GetKeyFlag( UserCode, dNr,85 );
	if ( result == 0 )
	{
		long keydata[4];
		keydata[0]=49038035;
		keydata[1]=740392783;
		keydata[2]=1294833;
		keydata[3]=93803484;

		result = rDongle_WriteKey(UserCode, keydata, dNr, 85 );
		if( result > 0 )
			printf("enckey written\n");
		else
			printf("enckey write error\n");
	}
	else
	{
		printf("GetKeyFlag is %d\n", result);
	}


	// no retry
	// Login
	bool bLogin=false;
	result = rLogIn_MatrixNet(UserCode,1, dNr);
	if( result < 0 )
		printf("Login Error %d\n", result );
	else
	{
		printf("LogIn dNr=%d appSlot=%d result=%d\n", dNr, 1, result);
		bLogin=true;
	}

	// no retry
	// Get Serial Number
	long ser = rDongle_ReadSerNr(UserCode, dNr,85);
	if ( ser < 0 )
	{
		printf( "SerNo Error %ld\n", ser);
	}
	else
		printf( "SerNo: %ld\n", ser);

	// no retry
	// Read & Write
	result = rDongle_ReadDataEx( UserCode,data, 7, 5, dNr, 85 );
	if ( result < 0 )
	{
		printf("data read error %d\n", result );
	}
	else
	{
		printf( "data read Count=%d\n", result);
		printf( "data %u %u %u %u %u\n", data[0], data[1], data[2], data[3], data[4]);

		data[0]++;
		data[1]++;
		data[2]++;
		data[3]++;
		data[4]++;
	
		// no retry
		result = rDongle_WriteDataEx(UserCode,data, 7, 5, dNr, 85);
		if( result < 0 )
			printf("data write error %d\n", result );
		else
			printf( "data write Count=%d\n", result);

	}


	// no retry
	// Encrypt
	data[0]=12345;
	data[1]=56789;
	result = rDongle_EncryptData(UserCode,data, dNr,85);
	if( result <  0 )
		printf("encrypt error %d\n", result );
	else
		printf( "12345 56789 ->data encrypted %u %u\n", data[0], data[1]);


	// no retry
	// Decrypt to see if the original data are returned
	result = rDongle_DecryptData(UserCode,data, dNr, 85);
	if( result > 0 )
		printf( "data decrypted -> %u %u\n", data[0], data[1]);
	else
		printf("encrypt error %d\n", result );

	// Logout only when logged in
	if( bLogin == true )
	{
		// retry sample
		// Once logged int, retry as many times as possible to log out
		// or a logout error will fail to restore the license count
		for( retry = 0 ; ; retry++ )
		{
			result = rLogOut_MatrixNet(UserCode, 1, dNr);
			if( result < 0 )
			{
				if( result == -100 && retry < RETRY_COUNT*2 )
					continue;
			}
			break;
		}
		if( result < 0 )
			printf("Logout error %d\n", result );
		else
			printf( "LogOut dNr=%d appSlot=%d result=%d\n", dNr, 1, result);
	}

	// End Session
	result = rRelease_MatrixAPI();
	if( result < 0 )
		printf("Release Error %d\n", result );
	else
		printf( "Released ok\n");

	return 0;
}



int _tmain(int argc, _TCHAR* argv[])
{
	short port;

	//
	// 2 dongles on the port 1 and 2 are available.
	// Note that they have different licensing schemes.
	//

	// repeat 15 times for the port 1
	port = 1;
	printf("port=%d\n", port);
	for( int n= 0 ; n < 15; n++ )
	{

		loop(1234, port);
		printf("--------------------------------------------------------\n");
	}

	// repeat 15 times for the port 2
	port = 2;
	printf("port=%d\n", port);
	for( int n= 0 ; n < 15; n++ )
	{

		loop(1234, port);
		printf("--------------------------------------------------------\n");
	}

}


