//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace FpgaMeasurementInstrumentsLib
{
	/// <summary>
	/// A collection of utilities.
	/// </summary>
	public static class HssUtilTs
	{

		public struct LocalBusParams
		{
			public int cardHandle;
			public int address;
			public int data;
		}

		#region PublicFuctions

		/// <summary>
		/// 
		/// </summary>
		/// <param name="chassisHandle"></param>
		/// <param name="errorCode"></param>
		/// <param name="moduleName"></param>
		/// <returns></returns>
		public static string BuildErrorString(uint chassisHandle, int errorCode, string moduleName)
		{
			StringBuilder errorStrTemp = new StringBuilder(512);

			int ret = HssubNativeMethods.terHss_error_message(chassisHandle, errorCode, errorStrTemp);

			if (ret != 0)
			{
				throw new Exception("HssUtilTs::BuildErrorString() - terHss_error_message returned an error(" + ret + ")");
			}

			string errorMsg = errorStrTemp.ToString();

			return errorMsg += "(" + moduleName + ")";
		}

      /// <summary>
      /// 
      /// </summary>
      /// <param name="data"></param>
      /// <returns></returns>
      public static LocalBusParams ByteArrayToLocalBusParms(byte[] data)
		{
			GCHandle rxPinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
			IntPtr pBytePtr = rxPinnedArray.AddrOfPinnedObject();
			LocalBusParams lbParams = (LocalBusParams)(Marshal.PtrToStructure(pBytePtr, typeof(LocalBusParams)));
			rxPinnedArray.Free();

			return lbParams;
		}

      /// <summary>
      /// 
      /// </summary>
      /// <param name="lbParams"></param>
      /// <returns></returns>
      public static byte[] LocalBusParmsToByteArray(LocalBusParams lbParams)
		{
			// get the size of the structure
			int structureSize = Marshal.SizeOf(lbParams);

			// allocate a byte array
			byte[] dataToSend = new byte[structureSize];

			// convert structure to byte array
			IntPtr pBytePtr = Marshal.AllocHGlobal(structureSize);
			Marshal.StructureToPtr(lbParams, pBytePtr, true);
			Marshal.Copy(pBytePtr, dataToSend, 0, structureSize);
			Marshal.FreeHGlobal(pBytePtr);

			return dataToSend;
		}

      /// <summary>
      /// 
      /// </summary>
      /// <param name="chassisHandle"></param>
      /// <param name="hssubSubSystemAppSync"></param>
      /// <param name="name"></param>
      public static void WaitForSync(uint chassisHandle, int hssubSubSystemAppSync, string name)
		{
			//0 is VI_FALSE, 1 is VI_TRUE
			int hssubSyncData = 0;
			int ret = HssubNativeMethods.terHss_Application_WaitForSyncObject(chassisHandle, hssubSubSystemAppSync, HssubNativeMethods.TERHSS_TIMEOUT_10SEC, 1, ref hssubSyncData);
			if (ret != 0)
			{
				string errorStr = HssUtilTs.BuildErrorString(chassisHandle, ret, name);

				throw new Exception("HssUtilTs::WaitForSync() - terHss_Application_WaitForSyncObject() returned an error(" + ret + ")" + ": " + errorStr);
			}
		}
		#endregion
	}
}
