// 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 FpgaMeasurementInstrumentsLib
{
///
/// An abstract base class for FPGACmdMessages that go between the client and server
///
internal abstract class FPGACmdMessage : ICloneable
{
#region PublicClassMembers
public enum Page
{
PAGE0,
PAGE1,
PAGE2,
PAGE3
}
#endregion
#region PrivateClassMembers
private readonly string m_description;
protected FPGACmdMessageHeader m_header;
protected static void SwapWord(ref byte[] input)
{
//Word Size
int WORD_SIZE = 4;
for (int i = 0; i < input.Length; i = i + WORD_SIZE)
{
//Temp array for swap
byte[] temp = new byte[WORD_SIZE];
//Copy a word into temp
Buffer.BlockCopy(input, i, temp, 0, WORD_SIZE);
//Swap bytes
Array.Reverse(temp);
//Replace word
Buffer.BlockCopy(temp, 0, input, i, WORD_SIZE);
}
}
#endregion
#region PrivateFuctions
///
/// A command message constructor for all children to invoke
///
/// The command message id
/// The page of the command message
/// The description of the command message
protected FPGACmdMessage(FPGACmdMsgIds commandType, Page page, string description)
{
try
{
m_description = description;
m_header = new FPGACmdMessageHeader(commandType, page);
}
catch (Exception)
{
throw;
}
}
///
/// Copy Constructor
///
/// The FPGACmdMessage to copy from
protected FPGACmdMessage(FPGACmdMessage FPGACmdMessage)
{
try
{
m_header = new FPGACmdMessageHeader(FPGACmdMessage.m_header);
m_description = FPGACmdMessage.m_description;
}
catch (Exception)
{
throw;
}
}
///
/// A clone function for the children to implement
///
/// a clone of the child object
protected abstract object CloneSelf();
///
/// A function to encode outgoing data
///
/// a pointer to the encoded data
protected abstract void FormatData(IntPtr pData);
///
///
///
///
protected abstract int GetPayloadSize();
///
/// a function to decode incoming data
///
/// A pointer to data to populate this object with
protected abstract void ParseData(IntPtr pData);
#endregion
#region PublicFuctions
///
/// Get a copy of the FPGACmdMessage object
///
/// A clone of this object
public object Clone()
{
// tell the child to clone itself
return this.CloneSelf();
}
///
/// Encode the FPGACmdMessage into a byte array for sending
///
/// The buffer to put the FPGACmdMessage items
public void Format(IntPtr pData)
{
try
{
m_header.Format(pData);
IntPtr pPayload = IntPtr.Add(pData, (int)GetHeaderLength());
// ask child class to format its data
FormatData(pPayload);
}
catch (Exception)
{
throw;
}
}
///
/// Getter for the description
///
/// The description
public string GetDescription()
{
return m_description;
}
///
/// getter for the message id
///
/// The id
public FPGACmdMsgIds GetMessageId()
{
return m_header.GetMessageId();
}
///
/// getter for the header length
///
/// The number of bytes in the header
public uint GetHeaderLength()
{
return m_header.GetHeaderLength();
}
///
///
///
///
public uint GetEntireMsgLength()
{
return m_header.GetHeaderLength() + (uint)GetPayloadSize();
}
///
/// Takes an array of bytes and populates the FPGACmdMessage object
///
/// The FPGACmdMessage in byte form
public void Parse(IntPtr pData)
{
try
{
m_header.Parse(pData);
IntPtr pPayLoad = IntPtr.Add(pData, (int)GetHeaderLength());
ParseData(pPayLoad);
}
catch (Exception)
{
throw;
}
}
///
/// Convert this FPGACmdMessage into string form
///
/// The FPGACmdMessage in string form
public override string ToString()
{
string desc = "Description: " + GetDescription() + "\n" + m_header.ToString();
return desc;
}
#endregion
}
}