Big changes

This commit is contained in:
Duc
2025-03-13 12:04:22 -07:00
parent c689fcb7f9
commit ffa9905494
748 changed files with 199255 additions and 3743 deletions

View File

@@ -0,0 +1,961 @@
// 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 Raytheon.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace BitMeasurementManagerLib
{
/// <summary>
/// This class implements a message that can be put on a communication interface
/// </summary>
public class BitConfigurableMessage
{
#region PublicClassMembers
public enum DataItemType
{
FLOAT,
UINT,
UINT_HEX,
USHORT_HEX,
USHORT,
BYTE_HEX,
BYTE_ARRAY_HEX,
DOUBLE,
ULONG,
ULONG_HEX,
BITS
}
#endregion
#region PrivateClassMembers
public List<IConfigurableDataItem> _dataItems;
public Dictionary<string, int> _byteArrayCount;
public Dictionary<string, int> _bitCount;
private string _messageName;
private uint _entireMsgLen;
private uint _messageId;
private uint _rspMessageId;
#endregion
#region PublicFuctions
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="rhs">The object to copy</param>
public BitConfigurableMessage(BitConfigurableMessage rhs)
{
// copy over the basic types
_messageId = rhs._messageId;
_rspMessageId = rhs._rspMessageId;
_messageName = String.Copy(rhs._messageName);
// _entireMsgLen will get set when we add the data items
_entireMsgLen = 0;
// initialize the objects
_dataItems = new List<IConfigurableDataItem>();
_byteArrayCount = new Dictionary<string, int>();
_bitCount = new Dictionary<string, int>();
// copy the data items over
foreach (IConfigurableDataItem dataItem in rhs._dataItems)
{
AddDataItem(dataItem.GetDataItemName(), dataItem.GetDataItemType(), dataItem.GetNumDataItems(), dataItem.GetDefaultValueStr());
}
}
/// <summary>
///
/// </summary>
/// <param name="messageId"></param>
/// <param name="rspMessageId"></param>
/// <param name="messageName"></param>
public BitConfigurableMessage(uint messageId, uint rspMessageId, string messageName)
{
_messageId = messageId;
_rspMessageId = rspMessageId;
_messageName = messageName;
_entireMsgLen = 0;
_dataItems = new List<IConfigurableDataItem>();
_byteArrayCount = new Dictionary<string, int>();
_bitCount = new Dictionary<string, int>();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public uint GetMessageId()
{
return _messageId;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public uint GetMessageRspId()
{
return _rspMessageId;
}
/// <summary>
///
/// </summary>
/// <param name="dataItemName"></param>
/// <param name="dataItemType"></param>
/// <param name="numItems"></param>
/// <param name="defaultValue"></param>
public void AddDataItem(string dataItemName, DataItemType dataItemType, int numItems, string defaultValue)
{
if (numItems < 1)
{
throw new Exception("BitConfigurableMessage::AddDataItem() - numItems is not valid: " + numItems + ", data item name is: " + dataItemName);
}
if (numItems > 1 && (dataItemType != DataItemType.BYTE_ARRAY_HEX && dataItemType != DataItemType.BITS))
{
throw new Exception("BitConfigurableMessage::AddDataItem() - numItems greater than one only allowed for hex byte arrays and BITs: " + numItems + ", data item name is: " + dataItemName);
}
if (dataItemType == DataItemType.UINT)
{
uint value = Convert.ToUInt32(defaultValue);
BitConfigurableMessageDataItem<uint> dataItem = new BitConfigurableMessageDataItem<uint>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(4 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.UINT_HEX)
{
uint value = Convert.ToUInt32(defaultValue, 16);
BitConfigurableMessageDataItem<uint> dataItem = new BitConfigurableMessageDataItem<uint>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(4 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.FLOAT)
{
float value = (float)Convert.ToDouble(defaultValue);
BitConfigurableMessageDataItem<float> dataItem = new BitConfigurableMessageDataItem<float>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(4 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.USHORT)
{
ushort value = Convert.ToUInt16(defaultValue);
BitConfigurableMessageDataItem<ushort> dataItem = new BitConfigurableMessageDataItem<ushort>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(2 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.USHORT_HEX)
{
ushort value = Convert.ToUInt16(defaultValue, 16);
BitConfigurableMessageDataItem<ushort> dataItem = new BitConfigurableMessageDataItem<ushort>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(2 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.DOUBLE)
{
double value = Convert.ToDouble(defaultValue);
BitConfigurableMessageDataItem<double> dataItem = new BitConfigurableMessageDataItem<double>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(8 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.ULONG)
{
ulong value = Convert.ToUInt64(defaultValue);
BitConfigurableMessageDataItem<ulong> dataItem = new BitConfigurableMessageDataItem<ulong>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(8 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.ULONG_HEX)
{
ulong value = Convert.ToUInt64(defaultValue, 16);
BitConfigurableMessageDataItem<ulong> dataItem = new BitConfigurableMessageDataItem<ulong>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(8 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.BYTE_HEX)
{
byte value = Convert.ToByte(defaultValue, 16);
BitConfigurableMessageDataItem<byte> dataItem = new BitConfigurableMessageDataItem<byte>(dataItemName, dataItemType, value, defaultValue, numItems);
_entireMsgLen += (uint)(1 * numItems);
_dataItems.Add(dataItem);
}
else if (dataItemType == DataItemType.BITS)
{
if (numItems > 8)
{
throw new Exception("BitConfigurableMessage::AddDataItem() - trying to add bit field: " + dataItemName + " that has more than 8 bits, bit fields are required to be less than 8 bits at this time");
}
// query how many bit fields have been added
// use this later when updating the number of bytes in the message
int totalBitPrevCount = 0;
foreach (KeyValuePair<string, int> entry in _bitCount)
{
totalBitPrevCount += entry.Value;
}
//intention integer division
uint numBytesPrevAdded = (uint)(totalBitPrevCount / 8);
int totalBitPrevCountRemander = totalBitPrevCount % 8;
// if remander is not 0, bump up the byte count by 1
if (totalBitPrevCountRemander != 0)
{
numBytesPrevAdded++;
}
// convert value to a byte and create the data item
defaultValue = defaultValue.Replace("0b", "");
defaultValue = defaultValue.Replace("0B", "");
byte value = Convert.ToByte(defaultValue, 2);
BitConfigurableMessageDataItem<byte> dataItem = new BitConfigurableMessageDataItem<byte>(dataItemName, dataItemType, value, defaultValue, numItems);
_dataItems.Add(dataItem);
// store the number of bits for this items
_bitCount[dataItemName.ToUpper()] = numItems;
// now that we updated the bit fields, query how many bit fields there are
int totalBitCount = 0;
foreach (KeyValuePair<string, int> entry in _bitCount)
{
totalBitCount += entry.Value;
}
// intentional integer division
uint numBytesToAdd = (uint)(totalBitCount / 8);
int totalBitCountRemander = totalBitCount % 8;
// if remander is not 0, bump up the byte count by 1
if (totalBitCountRemander != 0)
{
numBytesToAdd++;
}
// increment the message length we crossed a byte boundary with the total number of bits
_entireMsgLen += numBytesToAdd - numBytesPrevAdded;
}
else if (dataItemType == DataItemType.BYTE_ARRAY_HEX)
{
byte value = Convert.ToByte(defaultValue, 16);
byte[] data = new byte[numItems];
for (int i = 0; i < numItems; i++)
{
data[i] = value;
}
BitConfigurableMessageDataItem<Array> dataItem = new BitConfigurableMessageDataItem<Array>(dataItemName, dataItemType, data, defaultValue, numItems);
_dataItems.Add(dataItem);
_entireMsgLen += (uint)(1 * numItems);
_byteArrayCount[dataItemName.ToUpper()] = numItems;
}
else
{
throw new Exception("BitConfigurableMessage::AddDataItem() - Unsupported data item type: " + dataItemType.ToString());
}
}
/// <summary>
///
/// </summary>
/// <param name="pData"></param>
public void Format(IntPtr pData)
{
IntPtr pDataLocation = pData;
// should we byte swap
bool shallWeSwap = BitMsgEndianControl.Instance().ShallWeSwap();
// format the data
List<IConfigurableDataItem> formattedData = _dataItems;
for (int i = 0; i < formattedData.Count; i++)
{
IConfigurableDataItem dataItem = _dataItems[i];
DataItemType itemType = dataItem.GetDataItemType();
string itemName = dataItem.GetDataItemName();
object itemValue = dataItem.Data;
int dataItemSize = 0;
if (itemValue is float)// if (itemType == DataItemType.FLOAT)
{
float data = (float)itemValue;
if (shallWeSwap == true)
{
itemValue = Util.Swap(data);
}
else
{
itemValue = data;
}
dataItemSize = 4;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else if (itemValue is uint)
{
uint data = (uint)itemValue;
if (shallWeSwap == true)
{
itemValue = Util.Swap(data);
}
else
{
itemValue = data;
}
dataItemSize = 4;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else if (itemValue is ushort)
{
ushort data = (ushort)itemValue;
if (shallWeSwap == true)
{
itemValue = Util.Swap(data);
}
else
{
itemValue = data;
}
dataItemSize = 2;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else if (itemValue is byte)
{
// handle the case where we stored an array of BITS as a byte
if (itemType == DataItemType.BITS)
{
// grab the value of the first bit item
byte totalBitItemValue = (byte)itemValue;
// queue up the rest of the BITs to the next byte boundary
int totalBitsProcessed = dataItem.GetNumDataItems();
for (int j = i + 1; totalBitsProcessed < 8; j++)
{
// grab the next data items
IConfigurableDataItem bitDataItem = _dataItems[j];
DataItemType bitItemType = bitDataItem.GetDataItemType();
// the next item should be BITS
if (bitItemType != DataItemType.BITS)
{
throw new Exception("BitConfigurableMessage::Format() - Bit fields not consecutive out to byte boundary for msg ID: " + _messageId.ToString("X8"));
}
int bitItemNumBites = bitDataItem.GetNumDataItems();
byte bitItemValue = (byte)bitDataItem.Data;
uint mask = (uint)((1 << bitItemNumBites) - 1) << totalBitsProcessed;
// mask in the next bit value
byte valueToMaskIn = (byte)((bitItemValue << totalBitsProcessed) & mask);
totalBitItemValue = (byte)(totalBitItemValue | valueToMaskIn);
// increment the bit counter
totalBitsProcessed += bitItemNumBites;
// increment the outer loop counter since we just processed it
i++;
}
// double check that we are on a byte boundary
//int byteBoundaryRemainder = totalBitsProcessed % 8;
if (totalBitsProcessed != 8)
{
throw new Exception("BitConfigurableMessage::Format() - Bit fields not equal to 8 for msg ID: " + _messageId.ToString("X8"));
}
dataItemSize = 1;
// hold onto the total bit item value
itemValue = (byte)totalBitItemValue;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else
{
itemValue = (byte)itemValue;
dataItemSize = 1;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
}
else if (itemValue is double)
{
itemValue = (double)itemValue;
dataItemSize = 8;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else if (itemValue is ulong)
{
itemValue = (ulong)itemValue;
dataItemSize = 8;
//copy data from our data items, info the buffer
Marshal.StructureToPtr(itemValue, pDataLocation, true);
}
else if (itemValue is Array)
{
IEnumerable en = (IEnumerable)itemValue;
byte[] arrayTemp = en.OfType<byte>().ToArray();
dataItemSize = arrayTemp.Length;
Marshal.Copy(arrayTemp, 0, pDataLocation, dataItemSize);
}
else
{
throw new Exception("BitConfigurableMessage::Format() - Unsupported data item type: " + itemType.ToString());
}
// increment the pointer
pDataLocation = IntPtr.Add(pDataLocation, dataItemSize);
}
}
/// <summary>
///
/// </summary>
/// <param name="dataItemName"></param>
/// <returns></returns>
public byte[] GetDataItemByName_ByteArray(string dataItemName)
{
for (int i = 0; i < _dataItems.Count; i++)
{
if (_dataItems[i].GetDataItemName().ToUpper() == dataItemName.ToUpper())
{
if (_dataItems[i].GetDataItemType() != DataItemType.BYTE_ARRAY_HEX)
{
throw new Exception("BitConfigurableMessage::GetDataItemByName_ByteArray() - item was not a byte array: " + dataItemName);
}
IEnumerable en = (IEnumerable)_dataItems[i].Data;
byte[] dataToReturn = en.OfType<byte>().ToArray();
return dataToReturn;
}
}
throw new Exception("BitConfigurableMessage::GetDataItemByName_ByteArray() - Could not find data item: " + dataItemName);
}
/// <summary>
///
/// </summary>
/// <param name="dataItemName"></param>
/// <returns></returns>
public T GetDataItemByName<T>(string dataItemName)
{
for (int i = 0; i < _dataItems.Count; i++)
{
if (_dataItems[i].GetDataItemName().ToUpper() == dataItemName.ToUpper())
{
T value = (T)Convert.ChangeType(_dataItems[i].Data, typeof(T));
return value;
}
}
throw new Exception("BitConfigurableMessage::GetDataItemByName() - Could not find data item: " + dataItemName);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public uint GetEntireMsgLength()
{
return _entireMsgLen;
}
/// <summary>
///
/// </summary>
/// <param name="pData"></param>
public void Parse(IntPtr pData)
{
// should we byte swap
bool shallWeSwap = BitMsgEndianControl.Instance().ShallWeSwap();
IntPtr pMsgDataPtr = pData;
for (int i = 0; i < _dataItems.Count; i++)
{
IConfigurableDataItem item = _dataItems[i];
object itemData = item.Data;
if (itemData is float)
{
float itemValue = 0;
unsafe
{
float* pFloatItem = (float*)pMsgDataPtr;
itemValue = *pFloatItem;
}
if (shallWeSwap == true)
{
itemValue = Util.Swap(itemValue);
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 4);
}
else if (itemData is uint)
{
uint itemValue = 0;
unsafe
{
uint* pUInt32Item = (uint*)pMsgDataPtr;
itemValue = *pUInt32Item;
}
if (shallWeSwap == true)
{
itemValue = Util.Swap(itemValue);
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 4);
}
else if (itemData is ushort)
{
ushort itemValue = 0;
unsafe
{
ushort* pUInt16Item = (ushort*)pMsgDataPtr;
itemValue = *pUInt16Item;
}
if (shallWeSwap == true)
{
itemValue = Util.Swap(itemValue);
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 2);
}
else if (itemData is double)
{
double itemValue = 0;
unsafe
{
double* pDoubleItem = (double*)pMsgDataPtr;
itemValue = *pDoubleItem;
}
if (shallWeSwap == true)
{
itemValue = Util.Swap(itemValue);
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 8);
}
else if (itemData is ulong)
{
ulong itemValue = 0;
unsafe
{
ulong* pUlongItem = (ulong*)pMsgDataPtr;
itemValue = *pUlongItem;
}
if (shallWeSwap == true)
{
itemValue = Util.Swap(itemValue);
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 8);
}
else if (itemData is byte)
{
// handle the case where we are storing bit arrays as a byte
if (item.GetDataItemType() == DataItemType.BITS)
{
// grab the value of the first bit item
byte rawDataValue = 0;
// Grab the byte from the msg pointer
unsafe
{
byte* pUInt8Item = (byte*)pMsgDataPtr;
rawDataValue = *pUInt8Item;
}
// set the first data item
uint mask = (uint)((1 << item.GetNumDataItems()) - 1);
item.Data = (byte)(rawDataValue & mask);
_dataItems[i] = item;
int totalBitsProcessed = item.GetNumDataItems();
// set the rest of the data items
for (int j = i + 1; totalBitsProcessed < 8; j++)
{
IConfigurableDataItem bitDataItem = _dataItems[j];
DataItemType bitItemType = bitDataItem.GetDataItemType();
// the next item should be BITS
if (bitItemType != DataItemType.BITS)
{
throw new Exception("BitConfigurableMessage::Parse() - Bit fields not consecutive out to byte boundary for msg ID: " + _messageId.ToString("X8"));
}
// shift the data into the beggining of the byte and mask out the other bits
mask = (uint)((1 << bitDataItem.GetNumDataItems()) - 1);
bitDataItem.Data = (byte)((rawDataValue >> totalBitsProcessed) & mask);
_dataItems[j] = bitDataItem;
// increment the bit counter
totalBitsProcessed += bitDataItem.GetNumDataItems();
// increment the outer loop counter since we just processed it
i++;
}
// double check that we are on a byte boundary
//int byteBoundaryRemainder = totalBitsProcessed % 8;
if (totalBitsProcessed != 8)
{
throw new Exception("BitConfigurableMessage::Parse() - Bit fields not on byte boundary forequal to 8 msg ID: " + _messageId.ToString("X8"));
}
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 1);
}
else
{
byte itemValue = 0;
unsafe
{
byte* pUInt8Item = (byte*)pMsgDataPtr;
itemValue = *pUInt8Item;
}
item.Data = itemValue;
_dataItems[i] = item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 1);
}
}
else if (itemData is Array)
{
int numBytes = _byteArrayCount[item.GetDataItemName().ToUpper()];
byte[] data = new byte[numBytes];
unsafe
{
for (int byteCounter = 0; byteCounter < numBytes; byteCounter++)
{
byte* pUInt8Item = (byte*)pMsgDataPtr;
data[byteCounter] = *pUInt8Item;
pMsgDataPtr = IntPtr.Add(pMsgDataPtr, 1);
}
}
item.Data = data;
_dataItems[i] = item;
}
else
{
throw new Exception("BitConfigurableMessage::Parse() - Unsupported data item type: " + item.GetDataItemType().ToString());
}
}
}
/// <summary>
/// Set the params of the message.
/// This function will iterate over the list of params passed in and sequentially set the data items in the message starting at the data item at index 0
/// </summary>
/// <param name="messageParams">an array of params</param>
public void SetParams(params object[] messageParams)
{
if (messageParams.Length > _dataItems.Count)
{
throw new Exception("BitConfigurableMessage::SetParams() - number of supplied parameters: " + messageParams.Length.ToString() + ", is larger than the number of parameters in the definition file: " + _dataItems.Count.ToString());
}
for (int i = 0; i < messageParams.Length; i++)
{
IConfigurableDataItem item = _dataItems[i];
object itemData = item.Data;
if (itemData is uint)
{
item.Data = Convert.ToUInt32(messageParams[i]);
_dataItems[i] = item;
}
else if (itemData is float)
{
item.Data = Convert.ToDouble(messageParams[i]);
_dataItems[i] = item;
}
else if (itemData is ushort)
{
item.Data = Convert.ToUInt16(messageParams[i]);
_dataItems[i] = item;
}
else if (itemData is double)
{
item.Data = Convert.ToDouble(messageParams[i]);
_dataItems[i] = item;
}
else if (itemData is ulong)
{
item.Data = Convert.ToUInt64(messageParams[i]);
_dataItems[i] = item;
}
else if (itemData is byte)
{
item.Data = Convert.ToByte(messageParams[i]);
_dataItems[i] = item;
// doesnt matter if it is of types BITs or not, the configurable data item already knows
/*if (item.GetDataItemType() == DataItemType.BITS)
{
item.Data = Convert.ToByte(messageParams[i]);
_dataItems[i] = item;
}
else
{
item.Data = Convert.ToByte(messageParams[i]);
_dataItems[i] = item;
}*/
}
else if (itemData is Array)
{
// convert object to byte array
IEnumerable en = (IEnumerable)messageParams[i];
byte[] data = en.OfType<byte>().ToArray();
item.Data = data;
_dataItems[i] = item;
}
else
{
throw new Exception("BitConfigurableMessage::SetParams() - unknown type");
}
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
string dataToReturn = "Description: " + _messageName + "\r\n";
dataToReturn += "Payload Data:\r\n";
for (int i = 0; i < _dataItems.Count; i++)
{
IConfigurableDataItem dataItem = _dataItems[i];
DataItemType itemType = dataItem.GetDataItemType();
string itemName = dataItem.GetDataItemName();
object itemValue = dataItem.Data;
if (itemValue is uint)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
uint data = (uint)itemValue;
dataToReturn += " " + itemName + ":" + "0x" + data.ToString("X8") + "\r\n";
}
}
else if (itemValue is ushort)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
ushort data = (ushort)itemValue;
dataToReturn += " " + itemName + ":" + "0x" + data.ToString("X4") + "\r\n";
}
}
else if (itemValue is double)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
double data = (double)itemValue;
dataToReturn += " " + itemName + ":" + data + "\r\n";
}
}
else if (itemValue is ulong)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
ulong data = (ulong)itemValue;
dataToReturn += " " + itemName + ":" + data + "\r\n";
}
}
else if (itemValue is byte)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
if (itemType == DataItemType.BITS)
{
byte data = (byte)itemValue;
string binString = Convert.ToString(data, 2).PadLeft(dataItem.GetNumDataItems(), '0');
dataToReturn += " " + itemName + ":" + "0b" + binString + "\r\n";
}
else
{
byte data = (byte)itemValue;
dataToReturn += " " + itemName + ":" + "0x" + data.ToString("X2") + "\r\n";
}
}
}
else if (itemValue is Array)
{
string arrayDataStr = "";
if (itemValue == null)
{
arrayDataStr += " " + itemName + ":Not Set" + "\r\n";
}
else
{
IEnumerable en = (IEnumerable)itemValue;
byte[] arrayTemp = en.OfType<byte>().ToArray();
StringBuilder hex = new StringBuilder(arrayTemp.Length * 2);
foreach (byte b in arrayTemp)
hex.AppendFormat("{0:x2}", b);
arrayDataStr += " " + itemName + ":" + hex.ToString() + "\r\n";
}
dataToReturn += arrayDataStr;
}
else if (itemValue is float)
{
if (itemValue == null)
{
dataToReturn += " " + itemName + ":Not Set\r\n";
}
else
{
float data = (float)itemValue;
dataToReturn += " " + itemName + ":" + data + "\r\n";
}
}
else
{
dataToReturn += " " + itemName + ":" + "UNKNOWN TYPE:" + itemType + "\r\n";
}
}
return dataToReturn + "\r\n";
}
#endregion
}
}

View File

@@ -0,0 +1,114 @@
// 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;
namespace BitMeasurementManagerLib
{
/// <summary>
///
/// </summary>
/// <typeparam name="TData"></typeparam>
public class BitConfigurableMessageDataItem<TData> : IConfigurableDataItem<TData>
{
#region PublicClassMembers
#endregion
#region PrivateClassMembers
private string _dataItemName;
private int _numItems;
private string _defaultValueStr;
private BitConfigurableMessage.DataItemType _dataItemType;
#endregion
#region PublicFunctions
/// <summary>
///
/// </summary>
/// <param name="dataItemName"></param>
/// <param name="type"></param>
/// <param name="dataValue"></param>
public BitConfigurableMessageDataItem(string dataItemName, BitConfigurableMessage.DataItemType type, TData dataValue, string defaultValueStr, int numItems)
{
_dataItemName = dataItemName;
_dataItemType = type;
_defaultValueStr = defaultValueStr;
_numItems = numItems;
Data = dataValue;
}
/// <summary>
///
/// </summary>
public Type DataType
{
get { return typeof(TData); }
}
/// <summary>
///
/// </summary>
object IConfigurableDataItem.Data
{
get { return Data; }
set { Data = (TData)value; }
}
/// <summary>
///
/// </summary>
public TData Data { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetDataItemName()
{
return _dataItemName;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetDefaultValueStr()
{
return _defaultValueStr;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public BitConfigurableMessage.DataItemType GetDataItemType()
{
return _dataItemType;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public int GetNumDataItems()
{
return _numItems;
}
#endregion
}
}

View File

@@ -0,0 +1,213 @@
// 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 Raytheon.Common;
using System;
using System.Collections.Generic;
namespace BitMeasurementManagerLib
{
/// <summary>
/// This class creates BitConfigurableMessage objects
/// </summary>
internal class BitConfigurableMessageExcelFactory : BitConfigurableMessageFactory
{
#region PrivateClassMembers
private Dictionary<uint, BitConfigurableMessage> _messageDictionary;
#endregion
#region PrivateFuctions
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
public BitConfigurableMessageExcelFactory(string filename)
{
const int MIN_ROWS = 3;
const int MIN_COLS = 4;
ExcelReader reader = null;
try
{
_messageDictionary = new Dictionary<uint, BitConfigurableMessage>();
reader = new ExcelReader(filename);
List<string> sheetNamesList = reader.ReadAllSheetNames();
foreach (string sheet in sheetNamesList)
{
// read the whole sheet and split on the lines
string entireSheet = reader.ReadAllRows(sheet, 1, 1);
string[] allRows = entireSheet.Split('\n');
// check that there is the min number of rows
if (allRows.Length < MIN_ROWS)
{
throw new Exception("BitConfigurableMessageExcelFactory::BitConfigurableMessageExcelFactory() - Sheet ID: " + sheet + " does not have the min amount of rows: " + MIN_ROWS.ToString());
}
// grab the first line where the command ID should be
string messageIdLine = allRows[1];
string[] messageIdLineTokens = messageIdLine.Split(',');
// check for the min number of columns
if (messageIdLineTokens.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelFactory::BitConfigurableMessageExcelFactory() - Sheet ID: " + sheet + " line 1 does not have the min amount of Cols: " + MIN_COLS.ToString());
}
// grab the message ID
uint msgId = Convert.ToUInt32(messageIdLineTokens[3], 16);
// grab the line with the response msg ID
string rspMessageIdLine = allRows[2];
string[] rspMessageIdLineTokens = rspMessageIdLine.Split(',');
// check the number of cols
if (rspMessageIdLineTokens.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelFactory::BitConfigurableMessageExcelFactory() - Sheet ID: " + sheet + " line 2 does not have the min amount of Cols: " + MIN_COLS.ToString());
}
// grab the rsp msg ID
uint rspMsgId = Convert.ToUInt32(rspMessageIdLineTokens[3], 16);
// create the message
BitConfigurableMessage msg = new BitConfigurableMessage(msgId, rspMsgId, sheet);
// process the sheet to create the message object
// we already processed the first two lines, start at 3
for (int i = 3; i < allRows.Length; i++)
{
var line = allRows[i];
var values = line.Split(',');
if (values.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelFactory::BitConfigurableMessageExcelFactory() - Sheet ID: " + msgId.ToString("X8") + " does not have all of the fields populated");
}
string dataItemName = values[0].Trim().ToUpper();
string fieldType = values[1].Trim().ToUpper();
string numberOfItemsTemp = values[2].Trim().ToUpper();
numberOfItemsTemp = numberOfItemsTemp.Replace(",", "");
int numberOfItems = Convert.ToInt32(numberOfItemsTemp);
string defaultValue = values[3].Trim().ToUpper();
BitConfigurableMessage.DataItemType dataType = BitConfigurableMessage.DataItemType.FLOAT;
if (fieldType == BitInterfaceManagerConstants.FLOAT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.FLOAT;
}
else if (fieldType == BitInterfaceManagerConstants.UINT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.UINT;
}
else if (fieldType == BitInterfaceManagerConstants.UINT_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.UINT_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.USHORT_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.USHORT_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.USHORT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.USHORT;
}
else if (fieldType == BitInterfaceManagerConstants.BYTE_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BYTE_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.BYTE_ARRAY_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BYTE_ARRAY_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.DOUBLE_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.DOUBLE;
}
else if (fieldType == BitInterfaceManagerConstants.ULONG_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.ULONG;
}
else if (fieldType == BitInterfaceManagerConstants.ULONG_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.ULONG_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.BITS_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BITS;
}
else
{
throw new Exception("BitConfigurableMessageExcelFactory::BitConfigurableMessageExcelFactory() - Unknown type: " + values[1]);
}
msg.AddDataItem(dataItemName, dataType, numberOfItems, defaultValue);
}
_messageDictionary[msgId] = msg;
}
}
catch (Exception)
{
throw;
}
finally
{
if (reader != null)
{
reader.Dispose();
}
}
}
#endregion
#region PublicFuctions
/// <summary>
///
/// </summary>
/// <param name="messageId"></param>
/// <returns></returns>
protected override BitConfigurableMessage CreateMessage(uint messageId)
{
if (_messageDictionary.ContainsKey(messageId) == false)
{
throw new Exception("BitConfigurableMessageExcelFactory:CreateMessage() - unknown ID: " + messageId.ToString("X8") + ", make sure this ID is defined in the definition ini file");
}
return new BitConfigurableMessage(_messageDictionary[messageId]);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override Dictionary<uint, BitConfigurableMessage> GetAllMessages()
{
return _messageDictionary;
}
#endregion
}
}

View File

@@ -0,0 +1,204 @@
// 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 ExcelZipLib;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace BitMeasurementManagerLib
{
/// <summary>
/// This class creates BitConfigurableMessage objects
/// </summary>
internal class BitConfigurableMessageExcelZipFactory : BitConfigurableMessageFactory
{
#region PrivateClassMembers
private Dictionary<uint, BitConfigurableMessage> _messageDictionary;
#endregion
#region PrivateFuctions
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="msgIds"></param>
public BitConfigurableMessageExcelZipFactory(string filename)
{
const int MIN_ROWS = 3;
const int MIN_COLS = 4;
try
{
_messageDictionary = new Dictionary<uint, BitConfigurableMessage>();
List<worksheet> allWorkSheets = Workbook.Worksheets(filename);
foreach (worksheet sheet in allWorkSheets)
{
string test = sheet.ToString();
// check that there is the min number of rows
if (sheet.Rows.Count < MIN_ROWS)
{
throw new Exception("BitConfigurableMessageExcelZipFactory::BitConfigurableMessageExcelZipFactory() - Sheet ID: " + sheet + " does not have the min amount of rows: " + MIN_ROWS.ToString());
}
// grab the command ID Row
Row cmdIdRow = sheet.Rows[1];
if (cmdIdRow.Cells.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelZipFactory::BitConfigurableMessageExcelZipFactory() - Sheet ID: " + sheet + " line 1 does not have the min amount of Cols: " + MIN_COLS.ToString());
}
string msgIdStr = cmdIdRow.Cells[3].Text.Trim().ToUpper();
uint msgId = Convert.ToUInt32(msgIdStr, 16);
// grab the rsp ID Row
Row rspIdRow = sheet.Rows[2];
if (rspIdRow.Cells.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelZipFactory::BitConfigurableMessageExcelZipFactory() - Sheet ID: " + sheet + " line 1 does not have the min amount of Cols: " + MIN_COLS.ToString());
}
string rspMsgIdStr = rspIdRow.Cells[3].Text.Trim().ToUpper();
// grab the rsp msg ID
uint rspMsgId = Convert.ToUInt32(rspMsgIdStr, 16);
// create the message
BitConfigurableMessage msg = new BitConfigurableMessage(msgId, rspMsgId, msgId.ToString("X8"));
//remove header, cmd ID and rsp ID rows from worksheet
sheet.Rows.Remove(sheet.Rows[0]);
sheet.Rows.Remove(sheet.Rows[0]);
sheet.Rows.Remove(sheet.Rows[0]);
// process the sheet to create the message object
// start at 1 to skip the file header
foreach (Row row in sheet.Rows)
{
if (row.Cells.Length < MIN_COLS)
{
throw new Exception("BitConfigurableMessageExcelZipFactory::BitConfigurableMessageFactory() - Sheet ID: " + msgId.ToString("X8") + " does not have all of the fields populated");
}
string dataItemName = row.Cells[0].Text.Trim().ToUpper();
string fieldType = row.Cells[1].Text.Trim().ToUpper();
string numberOfItemsTemp = row.Cells[2].Text.Trim().ToUpper();
numberOfItemsTemp = numberOfItemsTemp.Replace(",", "");
int numberOfItems = Convert.ToInt32(numberOfItemsTemp);
string defaultValue = row.Cells[3].Text.Trim().ToUpper();
BitConfigurableMessage.DataItemType dataType = BitConfigurableMessage.DataItemType.FLOAT;
if (fieldType == BitInterfaceManagerConstants.FLOAT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.FLOAT;
}
else if (fieldType == BitInterfaceManagerConstants.UINT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.UINT;
}
else if (fieldType == BitInterfaceManagerConstants.UINT_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.UINT_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.USHORT_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.USHORT_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.USHORT_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.USHORT;
}
else if (fieldType == BitInterfaceManagerConstants.BYTE_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BYTE_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.BYTE_ARRAY_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BYTE_ARRAY_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.DOUBLE_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.DOUBLE;
}
else if (fieldType == BitInterfaceManagerConstants.ULONG_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.ULONG;
}
else if (fieldType == BitInterfaceManagerConstants.ULONG_HEX_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.ULONG_HEX;
}
else if (fieldType == BitInterfaceManagerConstants.BITS_DATA_TYPE.ToUpper())
{
dataType = BitConfigurableMessage.DataItemType.BITS;
}
else
{
throw new Exception("BitConfigurableMessageExcelZipFactory::BitConfigurableMessageExcelZipFactory() - Unknown type: " + row.FilledCells[0].ToString());
}
msg.AddDataItem(dataItemName, dataType, numberOfItems, defaultValue);
}
// hold onto the message object
_messageDictionary[msgId] = msg;
}
}
catch (Exception)
{
throw;
}
}
#endregion
#region PublicFuctions
/// <summary>
///
/// </summary>
/// <param name="messageId"></param>
/// <returns></returns>
protected override BitConfigurableMessage CreateMessage(uint messageId)
{
if (_messageDictionary.ContainsKey(messageId) == false)
{
throw new Exception("BitConfigurableMessageExcelZipFactory:CreateMessage() - unknown ID: " + messageId.ToString("X8") + ", make sure this ID is defined in the definition ini file");
}
return new BitConfigurableMessage(_messageDictionary[messageId]);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override Dictionary<uint, BitConfigurableMessage> GetAllMessages()
{
return _messageDictionary;
}
#endregion
}
}

View File

@@ -0,0 +1,77 @@
// 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 Raytheon.Common;
using System;
using System.Collections.Generic;
namespace BitMeasurementManagerLib
{
/// <summary>
/// This class creates BitConfigurableMessage objects
/// </summary>
internal abstract class BitConfigurableMessageFactory
{
internal enum FactoryType
{
EXCEL,
EXCEL_ZIP
};
private static BitConfigurableMessageFactory _bitFactoryInstance;
#region PublicFuctions
public static BitConfigurableMessageFactory Instance(string inputFileName = "", FactoryType type = FactoryType.EXCEL_ZIP)
{
if (_bitFactoryInstance == null)
{
if (type == FactoryType.EXCEL)
{
_bitFactoryInstance = new BitConfigurableMessageExcelFactory(inputFileName);
}
else if (type == FactoryType.EXCEL_ZIP)
{
_bitFactoryInstance = new BitConfigurableMessageExcelZipFactory(inputFileName);
}
else
{
throw new Exception("BitConfigurableMessageFactory::Instance() - unsupport input type: " + type.ToString());
}
}
return _bitFactoryInstance;
}
protected abstract Dictionary<uint, BitConfigurableMessage> GetAllMessages();
protected abstract BitConfigurableMessage CreateMessage(uint messageId);
public Dictionary<uint, BitConfigurableMessage> RetreiveAllMessages()
{
return _bitFactoryInstance.GetAllMessages();
}
public BitConfigurableMessage RetreiveMessage(uint messageId)
{
return _bitFactoryInstance.CreateMessage(messageId);
}
#endregion
}
}

View File

@@ -0,0 +1,205 @@
// 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 Raytheon.Common;
using System;
namespace BitMeasurementManagerLib
{
/// <summary>
/// This class implements a message header that is used by the BitConfigurableMessage
/// </summary>
public static class BitConfigurableMessageHeader
{
#region PrivateClassMembers
#endregion
#region PublicFuctions
/// <summary>
///
/// </summary>
/// <param name="pData"></param>
/// <returns></returns>
public static uint GetMessageId(IntPtr pData, uint numBytesInPdata)
{
return GetCmdMessageId(pData, numBytesInPdata);
}
/// <summary>
///
/// </summary>
/// <param name="pData"></param>
/// <param name="isSecondaryIdUsed"></param>
/// <returns></returns>
public static uint GetSecondaryMessageId(IntPtr pData, uint numBytesInPdata, out bool isSecondaryIdUsed)
{
return GetSecondaryCmdMessageId(pData, numBytesInPdata, out isSecondaryIdUsed);
}
/// <summary>
///
/// </summary>
/// <param name="pData"></param>
/// <param name="isSecondaryIdUsed"></param>
/// <returns></returns>
private static uint GetSecondaryCmdMessageId(IntPtr pData, uint numBytesInPdata, out bool isSecondaryIdUsed)
{
unsafe
{
isSecondaryIdUsed = false;
bool shallWeSwap = BitMsgEndianControl.Instance().ShallWeSwap();
BitMsgEndianControl.HeaderDef headerDef = BitMsgEndianControl.Instance().GetHeaderDef();
// is there enough data?
if (numBytesInPdata <= headerDef.secondaryMsgIdByteLocation + headerDef.secondaryMsgIdDataLen)
{
ErrorLogger.Instance().Write("BitConfigurableMessageHeader::GetSecondaryCmdMessageId() - not enough data form a header. Buffer contained " + Convert.ToString(numBytesInPdata) + " bytes, needed " + (headerDef.secondaryMsgIdByteLocation + headerDef.secondaryMsgIdDataLen).ToString() + " for a header", ErrorLogger.LogLevel.INFO);
throw new Exception("BitConfigurableMessageHeader::GetSecondaryCmdMessageId() - not enough data form a header. Buffer contained " + Convert.ToString(numBytesInPdata) + " bytes, needed " + (headerDef.secondaryMsgIdByteLocation + headerDef.secondaryMsgIdDataLen).ToString() + " for a header");
}
if (headerDef.secondaryMsgIdByteLocation == 0)
{
isSecondaryIdUsed = false;
return 0;
}
else
{
isSecondaryIdUsed = true;
uint cmdId = 0;
if (headerDef.secondaryMsgIdDataLen == 1)
{
byte* pCmdIdBytePtr = (byte*)pData.ToPointer() + headerDef.secondaryMsgIdByteLocation;
cmdId = pCmdIdBytePtr[0];
}
else if (headerDef.secondaryMsgIdDataLen == 2)
{
ushort* pCmdIdUshortPtr = (ushort*)pData.ToPointer() + headerDef.secondaryMsgIdByteLocation;
ushort cmdIdTemp = (ushort)pCmdIdUshortPtr[0];
if (shallWeSwap == true)
{
cmdIdTemp = Util.Swap(cmdIdTemp);
}
cmdId = cmdIdTemp;
}
else if (headerDef.secondaryMsgIdDataLen == 4)
{
uint* pCmdIdUInttPtr = (uint*)pData.ToPointer() + headerDef.secondaryMsgIdByteLocation;
cmdId = (uint)pCmdIdUInttPtr[0];
if (shallWeSwap == true)
{
cmdId = Util.Swap(cmdId);
}
}
else
{
throw new Exception("BitConfigurableMessageHeader::GetSecondaryCmdMessageId() - unhandled Cmd ID data length: " + headerDef.msgIdDataLen);
}
return cmdId;
}
}
}
/// <summary>
/// getter for the message id
/// </summary>
/// <returns>The id</returns>
private static uint GetCmdMessageId(IntPtr pData, uint numBytesInPdata)
{
unsafe
{
bool shallWeSwap = BitMsgEndianControl.Instance().ShallWeSwap();
BitMsgEndianControl.HeaderDef headerDef = BitMsgEndianControl.Instance().GetHeaderDef();
// is there enough data?
if (numBytesInPdata <= headerDef.msgIdByteLocation + headerDef.msgIdDataLen)
{
ErrorLogger.Instance().Write("BitConfigurableMessageHeader::GetCmdMessageId() - not enough data form a header. Buffer contained " + Convert.ToString(numBytesInPdata) + " bytes, needed " + (headerDef.msgIdByteLocation + headerDef.msgIdDataLen).ToString() + " for a header", ErrorLogger.LogLevel.INFO);
throw new Exception("BitConfigurableMessageHeader::GetCmdMessageId() - not enough data form a header. Buffer contained " + Convert.ToString(numBytesInPdata) + " bytes, needed " + (headerDef.msgIdByteLocation + headerDef.msgIdDataLen).ToString() + " for a header");
}
uint cmdId = 0;
if (headerDef.msgIdDataLen == 1)
{
byte* pCmdIdBytePtr = (byte*)pData.ToPointer() + headerDef.msgIdByteLocation;
cmdId = pCmdIdBytePtr[0];
}
else if (headerDef.msgIdDataLen == 2)
{
ushort* pCmdIdUshortPtr = (ushort*)pData.ToPointer() + headerDef.msgIdByteLocation;
ushort cmdIdTemp = (ushort)pCmdIdUshortPtr[0];
if (shallWeSwap == true)
{
cmdIdTemp = Util.Swap(cmdIdTemp);
}
cmdId = cmdIdTemp;
}
else if (headerDef.msgIdDataLen == 4)
{
uint* pCmdIdUInttPtr = (uint*)pData.ToPointer() + headerDef.msgIdByteLocation;
cmdId = (uint)pCmdIdUInttPtr[0];
if (shallWeSwap == true)
{
cmdId = Util.Swap(cmdId);
}
}
else
{
throw new Exception("BitConfigurableMessageHeader::GetCmdMessageId() - unhandled Cmd ID data length: " + headerDef.msgIdDataLen);
}
return cmdId;
}
}
/// <summary>
/// Getter for the header length
/// The Header is considered to be all of the data up to the message ID
/// </summary>
/// <returns>The header length in bytes. The head Length is the number of bytes from the beginning of the message through the message ID</returns>
public static uint GetHeaderSize()
{
BitMsgEndianControl.HeaderDef headerDef = BitMsgEndianControl.Instance().GetHeaderDef();
int idSize = headerDef.msgIdByteLocation + headerDef.msgIdDataLen;
return (uint)idSize;
}
#endregion
}
}

View File

@@ -0,0 +1,144 @@
// 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;
namespace BitMeasurementManagerLib
{
/// <summary>
///
/// </summary>
internal class BitMessageIDs
{
#region PrivateClassMembers
private Dictionary<uint, uint> _idToSizeMap;
private Dictionary<uint, uint> _cmdIdToRspIdMap;
private List<uint> _msgIds;
#endregion
#region PublicFuctions
/// <summary>
///
/// </summary>
internal BitMessageIDs()
{
_idToSizeMap = new Dictionary<uint, uint>();
_cmdIdToRspIdMap = new Dictionary<uint, uint>();
_msgIds = new List<uint>();
}
/// <summary>
///
/// </summary>
/// <param name="messageId"></param>
internal void AddId(uint messageId)
{
_idToSizeMap.Add(messageId, 0);
_msgIds.Add(messageId);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
internal bool ContainsId(uint id)
{
return _idToSizeMap.ContainsKey(id);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
internal List<uint> GetAllIds()
{
return _msgIds;
}
/// <summary>
///
/// </summary>
/// <param name="cmdId"></param>
/// <returns></returns>
internal uint GetResponseId(uint cmdId)
{
if (_cmdIdToRspIdMap.ContainsKey(cmdId) == false)
{
throw new Exception("BitMessageIDs::GetResponseId() - cmdIdToRspIdMap does not contains messageId: " + cmdId.ToString("X8"));
}
return _cmdIdToRspIdMap[cmdId];
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
internal uint GetSize(uint id)
{
if (_idToSizeMap.ContainsKey(id) == false)
{
throw new Exception("BitMessageIDs::GetSize() - _idToSizeMap does not contais messageId: " + id.ToString("X8"));
}
return _idToSizeMap[id];
}
/// <summary>
///
/// </summary>
/// <param name="cmdId"></param>
/// <returns></returns>
internal bool IsThereAResponseMessage(uint cmdId)
{
if (_cmdIdToRspIdMap.ContainsKey(cmdId) == false)
{
return false;
}
else
{
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="msgId"></param>
/// <param name="numBytes"></param>
internal void SetMsgSize(uint msgId, uint numBytes)
{
_idToSizeMap[msgId] = numBytes;
}
/// <summary>
///
/// </summary>
/// <param name="cmdId"></param>
/// <param name="rspId"></param>
internal void SetRspId(uint cmdId, uint rspId)
{
_cmdIdToRspIdMap[cmdId] = rspId;
}
#endregion
}
}