/*****************************************************************************/
/*  MATRIXDEMO.CS   MS-WINDOWS Win32 (95/98/ME/NT/2K/XP)                     */
/*                                                                           */
/*  (C) TDi GmbH                                                             */
/*                                                                           */
/*  Example for C#                                                           */
/*****************************************************************************/

using System;
using System.Windows.Forms; // MessageBox
using System.Runtime.InteropServices; // Marshalling
using MXAPI;

namespace matrix
{
	/// <summary>
	/// Summary description for MatrixDemo.
	/// </summary>
	public class MatrixDemo
	{
		private short RetCode;
		private int[] DataIn;
		private int[] DataOut;
		private int[] KeyData;
		private int[] DataBlock;
		private short DNG_Count;
		private short DNG_Nummer;
		private short DNG_Mem;
		private short DNG_MaxVar;
		private int DNG_Version;
		private int DNG_SerNr;
		private short VerMajor;
		private short VerMinor;
		private short AppSlot;
	
		// ***********************************************************
		// * In this example we use the demo UserCode 1234.          *
		// ***********************************************************
		const short UserCode = 1234;
		short DNG_Port = 85;

		public MatrixDemo()
		{
			DataIn = new int[256];
			DataOut = new int[256];
			KeyData = new int[4];
			DataBlock = new int[2];
		}

        public void SetDNGNum( short dng_no )
        {
            DNG_Nummer = dng_no;
        }

		public void Number_of_Dongles()
		{
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Search for number of Dongles at DNG_Port                *
			// ***********************************************************
			DNG_Count = Matrix.rDongle_Count(DNG_Port);
		
			if(DNG_Count <= 0)
				MessageBox.Show("リモートキーは見つかりません");
			else
				MessageBox.Show("キーが見つかりました: " + DNG_Count.ToString() + " 個");
		
			RetCode = Matrix.rRelease_MatrixAPI();
		}


		public void Memory_of_Dongle()
		{
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Read memory size of last Dongle at DNG_Port and         *
			// * calculate the maximum number of data fields             *
			// ***********************************************************
		
			DNG_Mem = Matrix.rDongle_MemSize(DNG_Nummer, DNG_Port);
		
			if(DNG_Mem <= 0)
			{
				MessageBox.Show("メモリーサイズ取得中にエラー発生!");
				return;
			}
		
			DNG_MaxVar = (short)(DNG_Mem / 4);
		
			MessageBox.Show("メモリーサイズ: " + DNG_MaxVar.ToString());	
			
			RetCode = Matrix.rRelease_MatrixAPI();
		}
		
		public void Version_of_Dongle()
		{
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Read Dongle version from last Dongle at DNG_Port        *
			// ***********************************************************
		
			DNG_Version = Matrix.rDongle_Version(DNG_Nummer, DNG_Port);
		
			if(DNG_Version <= 0)
			{
				MessageBox.Show("バージョン読み取りエラー!");
			}
			else
			{
				VerMinor = (short)(DNG_Version & 0xffff);
				VerMajor = (short)(DNG_Version >> 16);
				MessageBox.Show("バージョン: " + VerMajor.ToString() + "." + VerMinor.ToString());
			}

			RetCode = Matrix.rRelease_MatrixAPI();		
		}
	
		public void Read_Dongle_SerNr()
		{
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Read the SerialNo. of last Dongle at DNG_Port via       *
			// * UserCode and display.                                   *
			// ***********************************************************
		
			DNG_SerNr = Matrix.rDongle_ReadSerNr(UserCode, DNG_Nummer, DNG_Port);
		
			if(DNG_SerNr < 0)
			{
				MessageBox.Show("シリアル番号取得エラー: " + DNG_SerNr.ToString());
				RetCode = Matrix.rRelease_MatrixAPI();
				return;
			}
			MessageBox.Show("シリアル番号:  (dec) " + DNG_SerNr.ToString() + "    (hex) " + DNG_SerNr.ToString("X"));
		
			RetCode = Matrix.rRelease_MatrixAPI();
		
		}
	
		public void Read_Dongle_Data()
		{
			string MessageString;
			short  i;
		
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Read 3 data fields from last Dongle at DNG_Port via     *
			// * UserCode and display.                                   *
			// ***********************************************************
		
			RetCode = Matrix.rDongle_ReadData(UserCode, ref DataIn[0], 3, DNG_Nummer, DNG_Port);
		
			if(RetCode < 0)
			{

				MessageBox.Show("フィールドデータ読込エラー: " + RetCode.ToString());
				RetCode = Matrix.rRelease_MatrixAPI();
				return;
			}
		
			MessageString = "フィールドデータ: \n\n";
			for(i = 0; i <= 2; i++)
			{
				MessageString = MessageString + "フィールド" + (i + 1).ToString("D4") + ":  " + DataIn[i].ToString() + "\n";
			}
			MessageBox.Show(MessageString);
		
			RetCode = Matrix.rRelease_MatrixAPI();
		
		}
	
		public void Write_Dongle_Data()
		{
			short  i;

			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }

            // ***********************************************************
			// * Save 3 data fields in last Dongle at DNG_Port via       *
			// * UserCode. The values 101,102,103 are saved as example.  *
			// ***********************************************************
		
			for(i = 0; i <= 2; i++)
			{
				DataOut[i] = 101 + i;
			}

			RetCode = Matrix.rDongle_WriteData(UserCode, ref DataOut[0], 3, DNG_Nummer, DNG_Port);
		
			if(RetCode < 0)
				MessageBox.Show("フィールドデータ書込エラー: " + RetCode.ToString());
			else
                MessageBox.Show("フィールドデータ書込完了");
		
			RetCode = Matrix.rRelease_MatrixAPI();		
		}

        public void Dongle_WriteKey(int[] key)
        {
            short i;

            RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }

            // ***********************************************************
            // * Save 3 data fields in last Dongle at DNG_Port via       *
            // * UserCode. The values 101,102,103 are saved as example.  *
            // ***********************************************************

            for (i = 0; i < 4; i++)
            {
                KeyData[i] = key[i];
            }

            RetCode = Matrix.rDongle_WriteKey(UserCode, ref KeyData[0], DNG_Nummer, DNG_Port);

            if (RetCode < 0)
                MessageBox.Show("暗号鍵設定エラー: " + RetCode.ToString() );
            else
                MessageBox.Show("暗号鍵を設定しました");

            RetCode = Matrix.rRelease_MatrixAPI();
        }

		public void Dongle_Encrypt_Decrypt()
		{
			string MessageString;
		
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("エラー No. " + RetCode);
                return;
            }
		
			DataBlock[0] = 1234567890; // Clear Data
			DataBlock[1] = 1234567890; // Clear Data
		
			MessageString = "プレーンテキスト:   \t" + "(dec)  " + DataBlock[0].ToString() + "\t   " + DataBlock[1].ToString() + "\n\t\t(hex)  " + DataBlock[0].ToString("X") + "\t   " + DataBlock[1].ToString("X") + "\n\n";
		
	
			// ***********************************************************
			// * Encrypt DataBlock over the Dongle                       *
			// ***********************************************************
			RetCode = Matrix.rDongle_EncryptData(UserCode, ref DataBlock[0], DNG_Nummer, DNG_Port);
		
			if(RetCode < 0)
			{
				MessageBox.Show("エラー: " + RetCode.ToString());
				RetCode = Matrix.rRelease_MatrixAPI();
				return;
			}
		
			MessageString = MessageString + "暗号化後データ: \t(dec)  " + DataBlock[0].ToString() + "\t   " + DataBlock[1].ToString() + "\n\t\t(hex)  " + DataBlock[0].ToString("X") + "\t   " + DataBlock[1].ToString("X") + "\n\n";
		
			// ***********************************************************
			// * Decrypt DataBlock over the Dongle                       *
			// ***********************************************************
			RetCode = Matrix.rDongle_DecryptData(UserCode, ref DataBlock[0], DNG_Nummer, DNG_Port);
		
			if(RetCode < 0)
			{
				MessageBox.Show("エラー: " + RetCode.ToString());
				RetCode = Matrix.rRelease_MatrixAPI();
			}
		
			MessageBox.Show(MessageString + "復号化後データ: \t(dec)  " + DataBlock[0].ToString() + "\t   " + DataBlock[1].ToString() + "\n\t\t(hex)  " + DataBlock[0].ToString("X") + "\t   " + DataBlock[1].ToString("X") + "\n\n");
	
			RetCode = Matrix.rRelease_MatrixAPI();
		}
	
		public void Version_of_API()
		{
			int RetVer;
		
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// ***********************************************************
			// * Read the version of the Matrix-API                      *
			// ***********************************************************
			RetVer = Matrix.rGetVersionAPI();
		
			if(RetVer == 0)
				MessageBox.Show("APIバージョン取得エラー");
			else
			{
				VerMinor = (short)(RetVer & 0xffff);
				VerMajor = (short)(RetVer >> 16);
				MessageBox.Show("Matrix-APIバージョン: " + VerMajor.ToString() + "." + VerMinor.ToString());
			}		
		
			RetCode = Matrix.rRelease_MatrixAPI();
		
		}
	
		public void Dongle_Network()
		{

			// *************************************************************
			// * Network                                                   *
			// * Example: the dongle is prepared with the value 7 in       *
			// *          Var0002, meaning AppSlot=2 and max. Users = 7    *
			// *          (AppSlot2 = max. 7 Users)                        *
			// *************************************************************
		
			RetCode = Matrix.rInit_MatrixAPI();
            if (RetCode < 0)
            {
                MessageBox.Show("初期化エラー No. " + RetCode);
                return;
            }
		
			// *************************************************************
			// * Max. Users stored in AppSlot 2 (Var0002 of the dongle)    *
			// *************************************************************
			AppSlot = 2;
		
		
			// *************************************************************
			// * LogIn will lock one User-Slot and the returned value is   *
			// * the remaining free User-Slots (for our Example RetCode=6) *
			// *************************************************************
			RetCode = Matrix.rLogIn_MatrixNet(UserCode, AppSlot, DNG_Nummer);
			if(RetCode < 0)
			{
				if(RetCode == -31)
					MessageBox.Show("AppSlotに空きがありません.");
				else
					MessageBox.Show("LogInエラー:  " + RetCode.ToString());
			}	
			else
				MessageBox.Show("LogIn成功!  空User-Slots: " + RetCode.ToString() );
		
			// *************************************************************
			// * LogOut will free the User-Slot and the returned value is  *
			// * the remaining free User-Slots (for our Example RetCode=7) *
			// *************************************************************
			RetCode = Matrix.rLogOut_MatrixNet(UserCode, AppSlot, DNG_Nummer);
			if(RetCode < 0)
				MessageBox.Show("LogOut エラー:  " + RetCode.ToString());
			else
				MessageBox.Show("LogOut成功! 空User-Slots: " + RetCode.ToString());
		
		
			RetCode = Matrix.rRelease_MatrixAPI();
		
		}
	

	} // end class
} // end namespace

