//                           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 Raytheon.Instruments.GeneralIO;
using System.Collections.Generic;
using SIUSBXP_DLL;
using System.Collections.Specialized;
using NLog;
using Raytheon.Common;
using System.IO;
using System.Reflection;

namespace Raytheon.Instruments
{
   /// <summary>
   /// Class for controlling a Silicon Labs CP2108 UART GPIO device
   /// </summary>
   public class DIOSiUSBXp : IGeneralIO, IDisposable
   {
      #region PublicClassMembers
      #endregion

      #region PrivateClassMembers
      private IntPtr _handle;
      private uint _deviceNum;
      private State _state;
      private string _name;
      private SelfTestResult _selfTestResult;
      private object _syncObj = new Object();

      private int _numChannelPerPort = 8;
      private int _channelStartIndex = 0;
      private int _numInputChannels;
      private int _numOutputChannels;
      private bool _shallWeInitializeOutput = false;

      private Dictionary<string, IODatatypes.DIOChannelInfo> _signalNameToChannelMap = new Dictionary<string, IODatatypes.DIOChannelInfo>();

      /// <summary>
      /// NLog logger
      /// </summary>
      private readonly ILogger _logger;
      /// <summary>
      /// Raytheon configuration
      /// </summary>
      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      #endregion

      #region PrivateClassFunctions
      ~DIOSiUSBXp()
      {
         Dispose(false);
      }

      /// <summary>
      /// 
      /// </summary>
      private void CancelIo()
      {
         int ret = SIUSBXP.SI_CancelIo(_handle);

         if (ret != 0)
         {
            throw new Exception("call to cancel IO returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void CancelIoEx()
      {
         int ret = SIUSBXP.SI_CancelIoEx(_handle, IntPtr.Zero);

         if (ret != 0)
         {
            throw new Exception("call to cancel IO EX returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="numBytesInQueue"></param>
      /// <param name="queueStatus"></param>
      private void CheckRXQueue(ref uint numBytesInQueue, ref uint queueStatus)
      {
         int ret = SIUSBXP.SI_CheckRXQueue(_handle, ref numBytesInQueue, ref queueStatus);

         if (ret != 0)
         {
            throw new Exception("call to check Rx Queue returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void Close()
      {
         int ret = SIUSBXP.SI_Close(_handle);

         if (ret != 0)
         {
            throw new Exception("call to close returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="ioControlCode"></param>
      /// <param name="inBuffer"></param>
      /// <param name="bytesToRead"></param>
      /// <param name="outBuffer"></param>
      /// <param name="bytesToWrite"></param>
      /// <param name="bytesSucceeded"></param>
      /// <returns></returns>
      private void DeviceIOControl(uint ioControlCode, ref byte[] inBuffer, uint bytesToRead, ref byte[] outBuffer, uint bytesToWrite, ref uint bytesSucceeded)
      {
         int ret = SIUSBXP.SI_DeviceIOControl(_handle, ioControlCode, inBuffer, bytesToRead, outBuffer, bytesToWrite, ref bytesSucceeded);

         if (ret != 0)
         {
            throw new Exception("call to SI_DeviceIOControl returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// Dispose the object's resources
      /// </summary>
      /// <param name="disposing"></param>
      protected virtual void Dispose(bool disposing)
      {
         try
         {
            if (disposing)
            {
               if (_state == State.Ready)
               {
                  Close();

                  _state = State.Uninitialized;
               }
            }
         }
         catch (Exception)
         {
            try
            {
               //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
            }
            catch (Exception)
            {
               //Do not rethrow. Exception from error logger that has already been garbage collected.
            }
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void FlushBuffers()
      {
         int ret = SIUSBXP.SI_FlushBuffers(_handle, 1, 1);

         if (ret != 0)
         {
            throw new Exception("call to flush returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="product"></param>
      /// <param name="length"></param>
      /// <param name="convertToAscii"></param>
      private void GetDeviceProductString(ref byte[] product, ref byte length, bool convertToAscii)
      {
         int ret = SIUSBXP.SI_GetDeviceProductString(_handle, product, ref length, convertToAscii);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="highVersion"></param>
      /// <param name="lowVersion"></param>
      private void GetDLLVersion(ref uint highVersion, ref uint lowVersion)
      {
         int ret = SIUSBXP.SI_GetDLLVersion(ref highVersion, ref lowVersion);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="highVersion"></param>
      /// <param name="lowVersion"></param>
      private void GetDriverVersion(ref uint highVersion, ref uint lowVersion)
      {
         int ret = SIUSBXP.SI_GetDriverVersion(ref highVersion, ref lowVersion);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="LibVersion"></param>
      private void GetInterfaceNumber(ref byte LibVersion)
      {
         int ret = SIUSBXP.SI_GetInterfaceNumber(_handle, ref LibVersion);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="modemStatus"></param>
      private void GetModemStatus(ref byte modemStatus)
      {
         int ret = SIUSBXP.SI_GetModemStatus(_handle, ref modemStatus);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void GetNumDevices()
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="libVersion"></param>
      private void GetPartLibraryVersion(ref int libVersion)
      {
         int ret = SIUSBXP.SI_GetPartLibraryVersion(_handle, ref libVersion);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="partNumber"></param>
      private void GetPartNumber(ref byte partNumber)
      {
         int ret = SIUSBXP.SI_GetPartNumber(_handle, ref partNumber);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// Can use as self-test
      /// </summary>
      /// <returns></returns>
      private void GetProductString()
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="readTimeoutMs"></param>
      /// <param name="writeTimeoutMs"></param>
      private void GetTimeouts(ref uint readTimeoutMs, ref uint writeTimeoutMs)
      {
         int ret = SIUSBXP.SI_GetTimeouts(ref readTimeoutMs, ref writeTimeoutMs);
         if (ret != 0)
         {
            throw new Exception("call returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      private void Open()
      {
         int ret = SIUSBXP.SI_Open(_deviceNum, ref _handle);

         if (ret != 0)
         {
            throw new Exception("call to open returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="buff"></param>
      /// <param name="numBytesToRead"></param>
      /// <returns></returns>
      private uint Read(ref byte[] buff, uint numBytesToRead)
      {
         uint bytesRead = 0;
         //uint bytesToReadUint = Convert.ToUInt32(bytesToRead);
         int ret = SIUSBXP.SI_Read(_handle, buff, numBytesToRead, ref bytesRead, IntPtr.Zero);
         if (ret != 0)
         {
            throw new Exception("call to read latch returned error: " + ret.ToString() + " on card: " + _name);
         }

         if (numBytesToRead != bytesRead)
         {
            throw new Exception("Commanded  " + numBytesToRead + " bytes, received " + bytesRead + " bytes on card: " + _name);
         }

         return bytesRead;
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="latch"></param>
      /// <returns></returns>
      private void ReadLatch(ref byte latch)
      {
         int ret = SIUSBXP.SI_ReadLatch(_handle, ref latch);

         if (ret != 0)
         {
            throw new Exception("call to read latch returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="baudDivisor"></param>
      /// <returns></returns>
      private void SetBaudDivisor(ushort baudDivisor)
      {
         int ret = SIUSBXP.SI_SetBaudDivisor(_handle, baudDivisor);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="baudRate"></param>
      private void SetBaudRate(uint baudRate)
      {
         int ret = SIUSBXP.SI_SetBaudRate(_handle, baudRate);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 0 or 1
      /// </summary>
      /// <param name="breakState"></param>
      /// <returns></returns>
      private void SetBreak(ushort breakState)
      {
         int ret = SIUSBXP.SI_SetBreak(_handle, breakState);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// CTS
      ///     SI_STATUS_INPUT = 0x00;
      ///     SI_HANDSHAKE_LINE = 0x01;
      /// RTS
      ///     SI_HELD_INACTIVE = 0x00;
      ///     SI_HELD_ACTIVE = 0x01;
      ///     SI_FIRMWARE_CONTROLLED = 0x02;
      ///     SI_TRANSMIT_ACTIVE_SIGNAL = 0x03;
      /// DTR
      ///     SI_HELD_INACTIVE = 0x00;
      ///     SI_HELD_ACTIVE = 0x01;
      ///     SI_FIRMWARE_CONTROLLED = 0x02;
      /// DSR
      ///     SI_STATUS_INPUT = 0x00;
      ///     SI_HANDSHAKE_LINE = 0x01;
      /// DCD
      ///     SI_STATUS_INPUT = 0x00;
      ///     SI_HANDSHAKE_LINE = 0x01;
      /// FlowXonXoff
      ///     0 or 1
      /// </summary>
      /// <returns></returns>
      private void SetFlowControl(byte cts, byte rts, byte dtr, byte dsr, byte dcd, bool flowXOnOff)
      {
         int ret = SIUSBXP.SI_SetFlowControl(_handle, cts, rts, dtr, dsr, dcd, flowXOnOff);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// Set the line control
      /// Bits 0-3: Number of stop bits
      ///     0: 1 stop bit
      ///     1: 1.5 stop bits
      ///     2: 2 stop bits
      /// Bits 4-7: Parity
      ///     0: none
      ///     1: Odd
      ///     2: Even
      ///     3: Mark
      ///     4: Space
      /// Bits 8-15: Number of bits per word
      ///     5, 6, 7, or 8
      /// </summary>
      /// <returns></returns>
      private void SetLineControl(ushort lineControl)
      {
         int ret = SIUSBXP.SI_SetLineControl(_handle, lineControl);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="readTimeoutMs"></param>
      /// <param name="writeTimeoutMs"></param>
      private void SetTimeout(uint readTimeoutMs, uint writeTimeoutMs)
      {
         int ret = SIUSBXP.SI_SetTimeouts(readTimeoutMs, writeTimeoutMs);
         if (ret != 0)
         {
            throw new Exception("call to returned error: " + ret.ToString() + " on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="writeBuff"></param>
      /// <param name="numBytesToWrite"></param>
      private void Write(ref byte[] writeBuff, uint numBytesToWrite)
      {
         uint bytesWritten = 0;
         //uint bytesToWriteUint = Convert.ToUInt32(numBytesToWrite);
         int ret = SIUSBXP.SI_Write(_handle, writeBuff, numBytesToWrite, ref bytesWritten, IntPtr.Zero);
         if (ret != 0)
         {
            throw new Exception("call to write latch returned error: " + ret.ToString() + " on card: " + _name);
         }

         if (numBytesToWrite != bytesWritten)
         {
            throw new Exception("Commanded  " + numBytesToWrite + " bytes, wrote " + bytesWritten + " bytes on card: " + _name);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="mask"></param>
      /// <param name="latch"></param>
      /// <returns></returns>
      private void WriteLatch(byte mask, byte latch)
      {
         int ret = SIUSBXP.SI_WriteLatch(_handle, mask, latch);

         if (ret != 0)
         {
            throw new Exception("call to write latch returned error: " + ret.ToString() + " on card: " + _name);
         }
      }
      #endregion

      #region PublicClassFunctions

      /// <summary>
      /// DIOSiUSBXp factory constructor
      /// </summary>
      /// <param name="deviceName"></param>
      /// <param name="configurationManager"></param>
      public DIOSiUSBXp(string deviceName, IConfigurationManager configurationManager, ILogger logger)
      {
         Name = deviceName;

         _logger = logger;

         _configurationManager = configurationManager;
         _configuration = _configurationManager.GetConfiguration(Name);

         string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
         string dioModuleDefPath = _configuration.GetConfigurationValue(deviceName, ConfigXml.DIO_MODULE_DEF_FILEPATH.ToString());

         if (!Path.IsPathRooted(dioModuleDefPath))
            dioModuleDefPath = Path.GetFullPath(Path.Combine(assemblyFolder, dioModuleDefPath));

         IConfigurationFile dioModuleConfig = new ConfigurationFile(dioModuleDefPath);

         Boolean.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.SHALL_WE_DRIVE_OUTPUT_UPON_INITIALIZATION.ToString()), out _shallWeInitializeOutput);

         UInt32.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.DEVICE_NUMBER.ToString()), out _deviceNum);

         Int32.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.NUM_INPUT_CHANNELS.ToString()), out _numInputChannels);
         Int32.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.NUM_OUTPUT_CHANNELS.ToString()), out _numOutputChannels);

         Int32.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.NUM_CHANNELS_PER_PORT.ToString()), out _numChannelPerPort);
         Int32.TryParse(dioModuleConfig.ReadValue(Name, ConfigIni.CHANNEL_START_INDEX.ToString()), out _channelStartIndex);

         if (!(_channelStartIndex == 0 || _channelStartIndex == 1))
         {
            throw new Exception($"The value for key {ConfigIni.CHANNEL_START_INDEX.ToString()} in section {Name} must be 0 or 1 in {dioModuleDefPath}");
         }

         List<string> outputSignalNames = dioModuleConfig.ReadAllKeys($"{Name}.{ConfigIni.OUTPUT_SIGNALS}");
         List<string> intputSignalNames = dioModuleConfig.ReadAllKeys($"{Name}.{ConfigIni.INPUT_SIGNALS}");

         IODatatypes.DIOChannelInfo info;
         foreach (string signalName in outputSignalNames)
         {
            if (_signalNameToChannelMap.ContainsKey(signalName))
               throw new Exception($"Key {signalName} in section {Name}.{ConfigIni.OUTPUT_SIGNALS} conflicts with the same key defined in another section.");

            string iniLine = dioModuleConfig.ReadValue($"{Name}.{ConfigIni.OUTPUT_SIGNALS}", signalName);
            string[] infoTokens = iniLine.Split('|');
            if (infoTokens.Length != 2)
            {
               throw new Exception($"Key {signalName} in section {Name}.{ConfigIni.OUTPUT_SIGNALS} does not contain 2 tokens");
            }

            info.channelNumber = Convert.ToUInt32(infoTokens[0]);
            info.initialValue = Convert.ToInt32(infoTokens[1]);

            _signalNameToChannelMap[signalName] = info;
         }

         foreach (string signalName in intputSignalNames)
         {
            if (_signalNameToChannelMap.ContainsKey(signalName))
               throw new Exception($"Key {signalName} in section {Name}.{ConfigIni.INPUT_SIGNALS} conflicts with the same key defined in another section.");

            string iniLine = dioModuleConfig.ReadValue($"{Name}.{ConfigIni.INPUT_SIGNALS}", signalName);

            info.channelNumber = Convert.ToUInt32(iniLine);
            info.initialValue = -1;

            _signalNameToChannelMap[signalName] = info;
         }

         _handle = IntPtr.Zero;
         _state = State.Uninitialized;
         _selfTestResult = SelfTestResult.Unknown;
      }

      /// <summary>
      ///
      /// </summary>
      /// <param name="deviceName"></param>
      /// <param name="deviceNum"></param>
      /// <param name="inputPins"></param>
      /// <param name="outputPins"></param>
      public DIOSiUSBXp(string deviceName, uint deviceNum)
      {
         _deviceNum = deviceNum;
         _name = deviceName;
         _handle = IntPtr.Zero;
         _state = State.Uninitialized;
         _selfTestResult = SelfTestResult.Unknown;
         _logger = LogManager.GetCurrentClassLogger();

         //@@@ Do we need to pass in more args to configure the DIO (Baud?)
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool ClearErrors()
      {
         //could use this to cancel IO and flush the buffers
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      public string DetailedStatus
      {
         get
         {
            byte arrSize = 255;
            byte[] deviceStringArray = new byte[arrSize];
            GetDeviceProductString(ref deviceStringArray, ref arrSize, true);
            return "This is a Silicon Labs CP2108 device: " + deviceStringArray.ToString();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public bool DisplayEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// Dispose of this object.
      /// </summary>
      public void Dispose()
      {
         lock (_syncObj)
         {
            try
            {
               Dispose(true);

               GC.SuppressFinalize(this);
            }
            catch (Exception)
            {
               try
               {
                  //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
               }
               catch (Exception)
               {
                  //Do not rethrow. Exception from error logger that has already been garbage collected.
               }
            }
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public bool FrontPanelEnabled
      {
         get
         {
            throw new NotImplementedException();
         }

         set
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="signalName"></param>
      /// <returns></returns>
      public IODatatypes.BitState GetBitState(string signalName)
      {
         lock (_syncObj)
         {
            if (!_signalNameToChannelMap.ContainsKey(signalName))
               throw new Exception($"Signal name {signalName} doesn't exist for card: " + _name);

            if (_signalNameToChannelMap[signalName].channelNumber >= _numInputChannels || _signalNameToChannelMap[signalName].channelNumber < _channelStartIndex)
            {
               throw new Exception($"The output channel number {_signalNameToChannelMap[signalName].channelNumber} specified must be >= {_channelStartIndex} and < {_numInputChannels + _channelStartIndex} on card " + _name);
            }

            int bitIndex = (int)_signalNameToChannelMap[signalName].channelNumber - _channelStartIndex;

            byte latchValue = 0;

            ReadLatch(ref latchValue);

            BitVector32 bits = new BitVector32(latchValue);

            return (IODatatypes.BitState)(bits[bitIndex] ? 1 : 0);
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public InstrumentMetadata Info
      {
         get
         {
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Initialize()
      {
         lock (_syncObj)
         {
            if (_state == State.Uninitialized)
            {
               //@@@ call the other setup functions..Baud? Flow? Timeout? Others?
               Open();
               _state = State.Ready;
            }
            else
            {
               throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on card " + _name);
            }
         }
      }

      /// <summary>
      /// Return list of signal names
      /// </summary>
      public List<string> GetSignalNames()
      {
         return new List<string>(_signalNameToChannelMap.Keys);
      }

      /// <summary>
      /// 
      /// </summary>
      public string Name
      {
         get { return _name; }
         set { _name = value; }
      }

      /// <summary>
      /// 
      /// </summary>
      public uint NumberOfInputBits
      {
         get
         {
            return (uint)_numInputChannels;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public uint NumberOfOutputBits
      {
         get
         {
            return (uint)_numOutputChannels;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public SelfTestResult PerformSelfTest()
      {
         lock (_syncObj)
         {
            // card does not support self test
            //throw new NotImplementedException("card does not support self test" + " on card " + _name);
            throw new NotImplementedException();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Reset()
      {
         lock (_syncObj)
         {
            Close();
            Open();
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public SelfTestResult SelfTestResult
      {
         get
         {
            return _selfTestResult;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="signalName"></param>
      /// <param name="state"> high(open) or low(closed) </param>
      /// 
      public void SetBit(string signalName, IODatatypes.BitState state)
      {
         lock (_syncObj)
         {
            if (!_signalNameToChannelMap.ContainsKey(signalName))
               throw new Exception($"Signal name {signalName} doesn't exist for card: " + _name);

            if (_signalNameToChannelMap[signalName].channelNumber >= _numOutputChannels || _signalNameToChannelMap[signalName].channelNumber < _channelStartIndex)
            {
               throw new Exception($"The output channel number {_signalNameToChannelMap[signalName].channelNumber} specified must be >= {_channelStartIndex} and < {_numOutputChannels + _channelStartIndex} on card " + _name);
            }

            int bitIndex = (int)_signalNameToChannelMap[signalName].channelNumber - _channelStartIndex;

            byte mask = (byte)bitIndex;

            if (state == IODatatypes.BitState.High)
            {
               WriteLatch(mask, 1);
            }
            else
            {
               WriteLatch(mask, 0);
            }
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="bit"></param>
      public void SetTristate(string signalName)
      {
         lock (_syncObj)
         {
            //@@@@ Is there a way to do this?
         }
      }

      /// <summary>
      /// 
      /// </summary>
      public void Shutdown()
      {
         lock (_syncObj)
         {
            if (_state == State.Ready)
            {
               Close();
               _state = State.Uninitialized;
            }
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="bit"></param>
      /// <param name="frequencyInHz"></param>
      /// <param name="dutyCylePercentage"></param>
      public void StartClock(uint bit, double frequencyInHz, double dutyCylePercentage)
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      public void StopClock(uint bit)
      {
         throw new NotImplementedException();
      }

      /// <summary>
      /// 
      /// </summary>
      public State Status
      {
         get
         {
            return _state;
         }
      }
      #endregion
   }
}
