/*------------------------------------------------------------------------- // 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.Collections.Generic; using System.Linq; using System.Threading; using NLog; using Raytheon.Instruments; namespace ProgramLib { /// /// Send Test Messages to UUT via TCP sockets /// internal class PerformTacticalUartCommAction : BasicAction { #region PrivateClassMembers private static NLog.ILogger _logger; private ICommDevice _serialDevice; private List _receiveByteList = new List(); private int _expectedPbitAggregate; #endregion /// /// Constructor /// /// public PerformTacticalUartCommAction() { string expectedPbitAggregate; _logger = LogManager.GetCurrentClassLogger(); string serialDeviceInstanceName = Program.Instance().ProgramSpecificConfig.ReadValue("UART_INFO", "SERIAL_DEVICE_INSTANCE_NAME"); expectedPbitAggregate = Program.Instance().ProgramSpecificConfig.ReadValue("PBIT_INFO", "SUCCESSFUL_PBIT_AGGREGATE_VALUE"); _expectedPbitAggregate = Convert.ToInt32(expectedPbitAggregate, 16); _serialDevice = (ICommDevice)Program.Instance().InstrumentManager.GetGenericInstrument(serialDeviceInstanceName); } /// /// Run the action /// /// public override void Run() { try { _logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() method..."); if (Program.Instance().IsUutPwrOn) { SendHandshakeAndGetResponse(); SendAckNackAndGetResponse(); } else { throw new Exception("Cannot send message to UUT because UUT is not powered on."); } } catch (Exception) { throw; } } /// /// Send AckNack message and get response /// /// public void SendAckNackAndGetResponse() { UartAckNackMessage msg = new UartAckNackMessage(0x0C, 1); List msgByteList = Util.StructToByteList(msg).ToList(); // remove checksum byte msgByteList.RemoveAt(msgByteList.Count - 1); msgByteList.Add(Util.GetTwosComplimentChecksum(msgByteList.ToArray())); _serialDevice.Write(msgByteList.ToArray(), (uint)msgByteList.Count); _logger.Info($"Raw AckNackMsg Sent: {Util.ByteArrayToHexString(msgByteList.ToArray())}"); UartPbitResultMessage pbitMsg = new UartPbitResultMessage(); List pbitMsgByteList = Util.StructToByteList(pbitMsg).ToList(); _logger.Info($"Waiting for PBIT Result from UUT"); byte[] byteArray; DateTime startDateTime = DateTime.Now; TimeSpan ts = DateTime.Now - startDateTime; const int MAX_PBIT_WAIT_SEC = 7; List pBitReceivedByteList = new List(); bool pBitMsgFound = false; while (ts.TotalSeconds < MAX_PBIT_WAIT_SEC) { ts = DateTime.Now - startDateTime; if (TacticalUartReadThread._byteQueue.Count > 0) { TacticalUartReadThread._byteQueue.TryDequeue(out byteArray); _receiveByteList.AddRange(byteArray); } else Thread.Sleep(100); if (_receiveByteList.Count < pbitMsgByteList.Count) { continue; } int index = 0; bool needMoreData = false; do { if (_receiveByteList[index] == pbitMsg.Id) { pBitReceivedByteList = _receiveByteList.Skip(index).Take(_receiveByteList.Count - index).ToList(); if (pBitReceivedByteList.Count < pbitMsgByteList.Count) { _receiveByteList = pBitReceivedByteList; needMoreData = true; break; } pBitReceivedByteList = pBitReceivedByteList.Skip(0).Take(pbitMsgByteList.Count).ToList(); if (Util.GetTwosComplimentChecksum(pBitReceivedByteList.Skip(0).Take(pbitMsgByteList.Count - 1).ToArray()) != pBitReceivedByteList[pBitReceivedByteList.Count - 1]) { index++; } else { int numBytesToRemove = index + pBitReceivedByteList.Count; if (numBytesToRemove == _receiveByteList.Count) _receiveByteList.Clear(); else _receiveByteList = _receiveByteList.Skip(numBytesToRemove).Take(_receiveByteList.Count - numBytesToRemove).ToList(); pBitMsgFound = true; break; } } else index++; if (index == _receiveByteList.Count) { _receiveByteList.Clear(); needMoreData = true; break; } } while (true); if (!needMoreData) { break; } } if (pBitMsgFound) { _logger.Info($"Raw Msg Received: {Util.ByteArrayToHexString(pBitReceivedByteList.ToArray(), pbitMsgByteList.Count)}"); UartPbitResultMessage rsp = Util.ByteArrayToStruct(pBitReceivedByteList.ToArray()); _logger.Info($"PbitResult Fields: PbitFinalResult={rsp.PbitFinalResult}, PbitAggregate=0x{rsp.PbitAggregate.ToString("X4")}, GSP_Temp={rsp.GspTemp} C, Lifetime_Time={rsp.LifetimeTime} sec, Lifetime_Count={rsp.LifetimeCount}"); if (rsp.PbitAggregate != _expectedPbitAggregate) { _logger.Error("UUT's PBIT failed."); } } else { throw new Exception("Timed out waiting for PBIT result."); } } /// /// Send Handshake message and get response /// /// public void SendHandshakeAndGetResponse() { UartHandshakeMessage handshakeMsg = new UartHandshakeMessage(0xCCAAFFEEDDAAEEDD, 0xDDEEAADDEEFFAACC); List handshakeMsgByteList = Util.StructToByteList(handshakeMsg).ToList(); // remove checksum byte handshakeMsgByteList.RemoveAt(handshakeMsgByteList.Count - 1); handshakeMsgByteList.Add(Util.GetTwosComplimentChecksum(handshakeMsgByteList.ToArray())); _serialDevice.Write(handshakeMsgByteList.ToArray(), (uint)handshakeMsgByteList.Count); _logger.Info($"Raw HandshakeMsg Sent: {Util.ByteArrayToHexString(handshakeMsgByteList.ToArray())}"); _logger.Info($"Waiting for HandshakeMsg from UUT"); byte[] byteArray; DateTime startDateTime = DateTime.Now; TimeSpan ts = DateTime.Now - startDateTime; const int MAX_HANDSHAKE_WAIT_SEC = 7; List handshakeReceivedByteList = new List(); bool handshakeMsgFound = false; double secondCount = 0.0; while (ts.TotalSeconds < MAX_HANDSHAKE_WAIT_SEC) { ts = DateTime.Now - startDateTime; if (ts.TotalSeconds > secondCount) { _serialDevice.Write(handshakeMsgByteList.ToArray(), (uint)handshakeMsgByteList.Count); _logger.Info($"Raw HandshakeMsg Sent: {Util.ByteArrayToHexString(handshakeMsgByteList.ToArray())}"); secondCount = ts.TotalSeconds; } if (TacticalUartReadThread._byteQueue.Count > 0) { TacticalUartReadThread._byteQueue.TryDequeue(out byteArray); _receiveByteList.AddRange(byteArray); } else Thread.Sleep(100); if (_receiveByteList.Count < handshakeMsgByteList.Count) { continue; } int index = 0; bool needMoreData = false; do { if (_receiveByteList[index] == handshakeMsg.Id) { handshakeReceivedByteList = _receiveByteList.Skip(index).Take(_receiveByteList.Count - index).ToList(); if (handshakeReceivedByteList.Count < handshakeMsgByteList.Count) { _receiveByteList = handshakeReceivedByteList; needMoreData = true; break; } handshakeReceivedByteList = handshakeReceivedByteList.Skip(0).Take(handshakeMsgByteList.Count).ToList(); if (Util.GetTwosComplimentChecksum(handshakeReceivedByteList.Skip(0).Take(handshakeMsgByteList.Count - 1).ToArray()) != handshakeReceivedByteList[handshakeReceivedByteList.Count - 1]) { index++; } else { int numBytesToRemove = index + handshakeReceivedByteList.Count; if (numBytesToRemove == _receiveByteList.Count) _receiveByteList.Clear(); else _receiveByteList = _receiveByteList.Skip(numBytesToRemove).Take(_receiveByteList.Count - numBytesToRemove).ToList(); handshakeMsgFound = true; break; } } else index++; if (index == _receiveByteList.Count) { _receiveByteList.Clear(); needMoreData = true; break; } } while (true); if (!needMoreData) { break; } } if (handshakeMsgFound) { _logger.Info($"Raw Msg Received: {Util.ByteArrayToHexString(handshakeReceivedByteList.ToArray(), handshakeMsgByteList.Count)}"); UartHandshakeMessage rsp = Util.ByteArrayToStruct(handshakeReceivedByteList.ToArray()); _logger.Info($"Handshake Fields: Pattern1=0x{rsp.Pattern1.ToString("X8")}, Pattern2=0x{rsp.Pattern2.ToString("X8")}"); } else { throw new Exception("Timed out waiting for HandshakeMsg."); } } } }