Major upgrade

This commit is contained in:
Duc
2025-10-24 15:18:11 -07:00
parent fd85735c93
commit ce583d1664
478 changed files with 237518 additions and 47610 deletions

View File

@@ -1,360 +1,348 @@
// Ignore Spelling: Geu Sdlc
using System;
using Raytheon.GuidedElectronicsUnit;
using System.Threading;
using Raytheon.Common;
using NLog;
using Raytheon.Common;
using Raytheon.GuidedElectronicsUnit;
namespace Raytheon.Instruments
{
public class CommDeviceGeuSdlc : ICommDevice, IDisposable
{
#region PrivateClassMembers
public class CommDeviceGeuSdlc : ICommDevice, IDisposable
{
#region PrivateClassMembers
private GuidedElectronicsUnit.GuidedElectronicsUnit _guidanceElectronicsUnit;
private readonly bool _idQuery = false;
private readonly bool _reset = false;
private readonly SelfTestResult _selfTestResult;
private State _state;
private readonly string _instrumentDriverSetup;
private readonly int _pollingRate;
private readonly string _resourceName;
private static readonly object _syncObj = new object();
private GuidedElectronicsUnit.GuidedElectronicsUnit _guidanceElectronicsUnit;
private readonly bool _idQuery = false;
private readonly bool _reset = false;
private readonly SelfTestResult _selfTestResult;
private State _state;
private readonly string _instrumentDriverSetup;
private readonly int _pollingRate;
private static readonly object _syncObj = new object();
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Raytheon configuration
/// </summary>
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
private readonly ILogger _logger;
#endregion
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
#region PrivatelassFunctions
#endregion
/// <summary>
/// The Finalizer
/// </summary>
~CommDeviceGeuSdlc()
{
Dispose(false);
}
#region PrivatelassFunctions
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// close resources
try
{
if (_state == State.Ready)
{
_guidanceElectronicsUnit.Close();
_guidanceElectronicsUnit.Dispose();
_state = State.Uninitialized;
}
}
catch (Exception)
{
try
{
}
catch (Exception)
{
//Do not rethrow. Exception from error logger that has already been garbage collected
}
}
}
}
/// <summary>
/// The Finalizer
/// </summary>
~CommDeviceGeuSdlc()
{
Dispose(false);
}
/// <summary>
/// Some HSS corrections
/// </summary>
/// <param name="data"></param>
/// <param name="numDWords"></param>
private static unsafe void PerformHssSwirl(ref byte[] data, uint numDWords)
{
fixed (byte* pBytePtr = &data[0])
{
for (int i = 0; i < numDWords; i++)
{
// swap the first word
ushort* pWord1 = (ushort*)pBytePtr + (i * 2);
*pWord1 = Util.Swap(*pWord1);
//swap the second word
ushort* pWord2 = (ushort*)pBytePtr + ((i * 2) + 1);
*pWord2 = Util.Swap(*pWord2);
// now swap the dword
uint* pDWord = (uint*)pBytePtr + i;
*pDWord = Util.SwapHighAndLowBytes(*pDWord);
}
}
}
#endregion
#region PublicClassFunctions
bool IInstrument.DisplayEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
bool IInstrument.FrontPanelEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
InstrumentMetadata IInstrument.Info => throw new NotImplementedException();
/// <summary>
/// CommDevice factory constructor
/// </summary>
/// <param name="deviceName"></param>
/// <param name="configurationManager"></param>
public CommDeviceGeuSdlc(string deviceName, IConfigurationManager configurationManager, ILogger logger)
{
_resourceName = deviceName;
_guidanceElectronicsUnit = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
_logger = logger;
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
_idQuery = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "IdQuery", true);
_reset = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "Reset", true);
_instrumentDriverSetup = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "InstrumentDriverSetup", "");
_pollingRate = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "PollingRate", 10);
}
/// <summary>
/// The constructor. Does not initialize anything. Use Initialize() to create the handle to the hardware
/// </summary>
/// <param name="resourceName"></param>
/// <param name="idQuery"></param>
/// <param name="reset"></param>
public CommDeviceGeuSdlc(string resourceName, bool idQuery, bool reset, string instrumentDriverSetup = "", int pollingRate = 10)
{
_resourceName = resourceName;
_idQuery = idQuery;
_reset = reset;
_guidanceElectronicsUnit = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
_instrumentDriverSetup = instrumentDriverSetup;
_pollingRate = pollingRate;
_logger = LogManager.GetCurrentClassLogger();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
bool IInstrument.ClearErrors()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a HSS GEU SDLC Device called " + _resourceName;
}
}
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
public void Dispose()
{
try
{
Dispose(true);
GC.SuppressFinalize(this);
}
catch (Exception err)
{
_logger.Error(err.Message + "\r\n" + err.StackTrace);
}
}
/// <summary>
/// Create a handle to the hardware
/// </summary>
public void Initialize()
{
const uint AUTO_FWDING_ADDR = 0x00080100;
const uint WRITE_OFFSET = 0x00200000;
const uint ENABLE_AUTO_FWDING = 0b0000_0001;
const uint ENABLE_TLP_HDR = 0b0000_0010;
//const uint INSERT_MSG_CNT = 0b0000_0100;
_guidanceElectronicsUnit = new GuidedElectronicsUnit.GuidedElectronicsUnit(_resourceName, _idQuery, _reset,
$"QueryInstrStatus=true, Simulate=false, DriverSetup= {_instrumentDriverSetup}, PollingInterval={_pollingRate}");
_guidanceElectronicsUnit.LowLevel.HSSub9100.EnableIO = ControlState.Enabled;
_guidanceElectronicsUnit.LowLevel.HSSub9100.WriteRegister(AUTO_FWDING_ADDR + WRITE_OFFSET, ENABLE_AUTO_FWDING | ENABLE_TLP_HDR);
_state = State.Ready;
}
/// <summary>
///
/// </summary>
public string Name
{
get
{
return _resourceName;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
SelfTestResult IInstrument.PerformSelfTest()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="dataRead"></param>
/// <returns></returns>
uint ICommDevice.Read(ref byte[] dataRead)
{
if (_guidanceElectronicsUnit == null)
{
return 0;
}
byte[] sdlcMsgs = new byte[0];
lock (_syncObj)
{
// read all of the data that is available
sdlcMsgs = _guidanceElectronicsUnit.GsKwSyncronousDataLinkControl.FetchMessageData();
}
if (sdlcMsgs.Length > dataRead.Length)
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// close resources
try
{
throw new Exception("The data buffer that the host provided is: " + dataRead.Length + " bytes, there are: " + sdlcMsgs.Length + " bytes of SDLC data. Need to increase the host buffer size");
if (_state == State.Ready)
{
_guidanceElectronicsUnit.Close();
_guidanceElectronicsUnit.Dispose();
_state = State.Uninitialized;
}
}
Buffer.BlockCopy(sdlcMsgs, 0, dataRead, 0, sdlcMsgs.Length);
catch (Exception)
{
try
{
}
catch (Exception)
{
//Do not rethrow. Exception from error logger that has already been garbage collected
}
}
}
}
return (uint)sdlcMsgs.Length;
}
/// <summary>
/// Some HSS corrections
/// </summary>
/// <param name="data"></param>
/// <param name="numDWords"></param>
private static unsafe void PerformHssSwirl(ref byte[] data, uint numDWords)
{
fixed (byte* pBytePtr = &data[0])
{
for (int i = 0; i < numDWords; i++)
{
// swap the first word
ushort* pWord1 = (ushort*)pBytePtr + (i * 2);
*pWord1 = Util.Swap(*pWord1);
/// <summary>
///
/// </summary>
void IInstrument.Reset()
{
lock (_syncObj)
{
_guidanceElectronicsUnit.Close();
_state = State.Uninitialized;
Thread.Sleep(500);
Initialize();
}
}
//swap the second word
ushort* pWord2 = (ushort*)pBytePtr + ((i * 2) + 1);
*pWord2 = Util.Swap(*pWord2);
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
return _selfTestResult;
}
}
// now swap the dword
uint* pDWord = (uint*)pBytePtr + i;
*pDWord = Util.SwapHighAndLowBytes(*pDWord);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="timeoutMs"></param>
void ICommDevice.SetReadTimeout(uint timeoutMs)
{
throw new NotImplementedException();
}
#endregion
/// <summary>
///
/// </summary>
void IInstrument.Shutdown()
{
lock (_syncObj)
{
if (_guidanceElectronicsUnit != null)
{
_guidanceElectronicsUnit.LowLevel.HSSub9100.EnableIO = ControlState.Disabled;
#region PublicClassFunctions
_guidanceElectronicsUnit.Close();
_guidanceElectronicsUnit.Dispose();
_state = State.Uninitialized;
}
}
}
bool IInstrument.DisplayEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
bool IInstrument.FrontPanelEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
InstrumentMetadata IInstrument.Info => throw new NotImplementedException();
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
/// CommDevice factory constructor
/// </summary>
/// <param name="deviceName"></param>
/// <param name="configurationManager"></param>
public CommDeviceGeuSdlc(string deviceName, IConfigurationManager configurationManager)
{
Name = deviceName;
_guidanceElectronicsUnit = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="numBytesToWrite"></param>
/// <returns></returns>
uint ICommDevice.Write(byte[] data, uint numBytesToWrite)
{
lock (_syncObj)
{
if (numBytesToWrite % 4 != 0)
{
throw new Exception("Data is not dword aligned");
}
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
// do all of the HSS Tx only byte order corrections
PerformHssSwirl(ref data, numBytesToWrite / 4);
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
var tempArr = new uint[data.Length / 4];
Buffer.BlockCopy(data, 0, tempArr, 0, (int)numBytesToWrite);
_guidanceElectronicsUnit.GsKwSyncronousDataLinkControl.SendMessage(tempArr);
}
return numBytesToWrite;
}
_idQuery = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "IdQuery", true);
_reset = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "Reset", true);
_instrumentDriverSetup = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "InstrumentDriverSetup", "");
_pollingRate = _configuration.GetConfigurationValue("CommDeviceGeuSdlc", "PollingRate", 10);
}
public void Close()
{
}
/// <summary>
/// The constructor. Does not initialize anything. Use Initialize() to create the handle to the hardware
/// </summary>
/// <param name="deviceName"></param>
/// <param name="idQuery"></param>
/// <param name="reset"></param>
public CommDeviceGeuSdlc(string deviceName, bool idQuery, bool reset, string instrumentDriverSetup = "", int pollingRate = 10)
{
Name = deviceName;
_idQuery = idQuery;
_reset = reset;
_guidanceElectronicsUnit = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
_instrumentDriverSetup = instrumentDriverSetup;
_pollingRate = pollingRate;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
}
public void Open()
{
}
/// <summary>
///
/// </summary>
/// <returns></returns>
bool IInstrument.ClearErrors()
{
throw new NotImplementedException();
}
#endregion
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a HSS GEU SDLC Device called " + Name;
}
}
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
public void Dispose()
{
try
{
Dispose(true);
GC.SuppressFinalize(this);
}
catch (Exception err)
{
_logger.Error(err.Message + "\r\n" + err.StackTrace);
}
}
/// <summary>
/// Create a handle to the hardware
/// </summary>
public void Initialize()
{
const uint AUTO_FWDING_ADDR = 0x00080100;
const uint WRITE_OFFSET = 0x00200000;
const uint ENABLE_AUTO_FWDING = 0b0000_0001;
const uint ENABLE_TLP_HDR = 0b0000_0010;
//const uint INSERT_MSG_CNT = 0b0000_0100;
_guidanceElectronicsUnit = new GuidedElectronicsUnit.GuidedElectronicsUnit(Name, _idQuery, _reset,
$"QueryInstrStatus=true, Simulate=false, DriverSetup= {_instrumentDriverSetup}, PollingInterval={_pollingRate}");
_guidanceElectronicsUnit.LowLevel.HSSub9100.EnableIO = ControlState.Enabled;
_guidanceElectronicsUnit.LowLevel.HSSub9100.WriteRegister(AUTO_FWDING_ADDR + WRITE_OFFSET, ENABLE_AUTO_FWDING | ENABLE_TLP_HDR);
_state = State.Ready;
}
/// <summary>
///
/// </summary>
public string Name { get; protected set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
SelfTestResult IInstrument.PerformSelfTest()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="dataRead"></param>
/// <returns></returns>
uint ICommDevice.Read(ref byte[] dataRead)
{
if (_guidanceElectronicsUnit == null)
{
return 0;
}
byte[] sdlcMsgs = new byte[0];
lock (_syncObj)
{
// read all of the data that is available
sdlcMsgs = _guidanceElectronicsUnit.GsKwSyncronousDataLinkControl.FetchMessageData();
}
if (sdlcMsgs.Length > dataRead.Length)
{
throw new Exception("The data buffer that the host provided is: " + dataRead.Length + " bytes, there are: " + sdlcMsgs.Length + " bytes of SDLC data. Need to increase the host buffer size");
}
Buffer.BlockCopy(sdlcMsgs, 0, dataRead, 0, sdlcMsgs.Length);
return (uint)sdlcMsgs.Length;
}
/// <summary>
///
/// </summary>
void IInstrument.Reset()
{
lock (_syncObj)
{
_guidanceElectronicsUnit.Close();
_state = State.Uninitialized;
Thread.Sleep(500);
Initialize();
}
}
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
return _selfTestResult;
}
}
/// <summary>
///
/// </summary>
/// <param name="timeoutMs"></param>
void ICommDevice.SetReadTimeout(uint timeoutMs)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
void IInstrument.Shutdown()
{
lock (_syncObj)
{
if (_guidanceElectronicsUnit != null)
{
_guidanceElectronicsUnit.LowLevel.HSSub9100.EnableIO = ControlState.Disabled;
_guidanceElectronicsUnit.Close();
_guidanceElectronicsUnit.Dispose();
_state = State.Uninitialized;
}
}
}
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="numBytesToWrite"></param>
/// <returns></returns>
uint ICommDevice.Write(byte[] data, uint numBytesToWrite)
{
lock (_syncObj)
{
if (numBytesToWrite % 4 != 0)
{
throw new Exception("Data is not dword aligned");
}
// do all of the HSS Tx only byte order corrections
PerformHssSwirl(ref data, numBytesToWrite / 4);
var tempArr = new uint[data.Length / 4];
Buffer.BlockCopy(data, 0, tempArr, 0, (int)numBytesToWrite);
_guidanceElectronicsUnit.GsKwSyncronousDataLinkControl.SendMessage(tempArr);
}
return numBytesToWrite;
}
public void Close()
{
}
public void Open()
{
}
#endregion
}
}

View File

@@ -32,71 +32,64 @@
// **********************************************************************************************************
// Ignore Spelling: Sdlc Geu
using NLog;
using Raytheon.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using NLog;
using Raytheon.Common;
namespace Raytheon.Instruments
{
[ExportInstrumentFactory(ModelNumber = "CommDeviceGeuSdlcFactory")]
public class CommDeviceGeuSdlcFactory : IInstrumentFactory
{
/// <summary>
/// The supported interfaces
/// </summary>
private readonly List<Type> _supportedInterfaces = new List<Type>();
private ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
private static string DefaultPath;
[ExportInstrumentFactory(ModelNumber = "CommDeviceGeuSdlcFactory")]
public class CommDeviceGeuSdlcFactory : IInstrumentFactory
{
private readonly List<Type> _supportedInterfaces = new List<Type>();
public CommDeviceGeuSdlcFactory(string defaultConfigPath = DefaultConfigPath)
: this(null, defaultConfigPath)
{
}
private readonly IConfigurationManager _configurationManager;
private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
private static string DefaultPath;
/// <summary>
/// COECommDeviceInstrumentFactory injection constructor
/// </summary>
/// <param name="configManager"></param>
/// <param name="simEngine"></param>
/// <param name="logger"></param>
[ImportingConstructor]
public CommDeviceGeuSdlcFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null )
{
DefaultPath = defaultConfigPath;
public CommDeviceGeuSdlcFactory(string defaultConfigPath = DefaultConfigPath)
: this(null, defaultConfigPath)
{
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
/// <summary>
/// CommDeviceGeuSdlcFactory injection constructor
/// </summary>
[ImportingConstructor]
public CommDeviceGeuSdlcFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(ICommDevice));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new CommDeviceGeuSdlc(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(ICommDevice));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
return new CommDeviceGeuSdlc(name, _configurationManager);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets the instrument
@@ -107,12 +100,10 @@ namespace Raytheon.Instruments
{
try
{
_logger = LogManager.GetLogger(name);
if (simulateHw)
return new CommDeviceSim(name, _configurationManager, _logger);
return new CommDeviceSim(name, _configurationManager);
else
return new CommDeviceGeuSdlc(name, _configurationManager, _logger);
return new CommDeviceGeuSdlc(name, _configurationManager);
}
catch (Exception)
{
@@ -125,17 +116,17 @@ namespace Raytheon.Instruments
/// </summary>
/// <returns></returns>
public ICollection<Type> GetSupportedInterfaces()
{
return _supportedInterfaces.ToArray();
}
{
return _supportedInterfaces.ToArray();
}
/// <summary>
/// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
/// </summary>
/// <returns></returns>
private static IConfigurationManager GetConfigurationManager()
{
return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
}
}
/// <summary>
/// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
/// </summary>
/// <returns></returns>
private static IConfigurationManager GetConfigurationManager()
{
return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
}
}
}