//******************************************************************************************************
// ChannelBase.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 3/7/2005 - J. Ritchie Carroll
// Generated original version of source code.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using GSF.Parsing;
namespace GSF.PhasorProtocols
{
///
/// Represents the common implementation of the protocol independent definition of any kind
/// of data that can be parsed or generated.
/// This is the base class of all parsing/generating classes in the phasor protocols library;
/// it is the root of the parsing/generating class hierarchy.
///
///
/// This base class represents data images for parsing or generation in
/// terms of a header, body and footer (see for details).
///
public abstract class ChannelBase : BinaryImageBase, IChannel
{
#region [ Members ]
// Fields
private Dictionary m_attributes; // Attributes dictionary
#endregion
#region [ Properties ]
///
/// Gets or sets the parsing state for this object.
///
public virtual IChannelParsingState State { get; set; }
///
/// Gets a of string based property names and values for this object.
///
///
/// The attributes dictionary is relevant to all channel properties. This dictionary will only be instantiated with a call to
/// the Attributes property which will begin the enumeration of relevant system properties. This is typically used for
/// display purposes. For example, this information is displayed in a tree view on the PMU Connection Tester to display
/// attributes of data elements that may be protocol specific.
///
public virtual Dictionary Attributes
{
get
{
// Create a new attributes dictionary or clear the contents of any existing one
if (m_attributes is null)
m_attributes = new Dictionary();
else
m_attributes.Clear();
m_attributes.Add("Derived Type", GetType().FullName);
m_attributes.Add("Binary Length", BinaryLength.ToString());
return m_attributes;
}
}
///
/// Gets or sets a user definable reference to an object associated with this object.
///
public virtual object Tag { get; set; }
///
/// Gets the binary image of the object.
///
///
/// This property is not typically overridden since it is the combination of the header, body and footer images.
///
public virtual byte[] BinaryImage
{
// This proxy property could be removed if all channel base implementations were recoded to
// implement GenerateHeaderImage, GenerateBodyImage, and GenerateFooterImage overrides...
get
{
byte[] buffer = new byte[BinaryLength];
int index = 0;
// Copy in header, body and footer images
int headerLength = HeaderLength;
if (headerLength > 0)
{
byte[] headerImage = HeaderImage;
if (headerImage is not null)
{
Buffer.BlockCopy(headerImage, 0, buffer, index, headerLength);
index += headerLength;
}
}
int bodyLength = BodyLength;
if (bodyLength > 0)
{
byte[] bodyImage = BodyImage;
if (bodyImage is not null)
{
Buffer.BlockCopy(bodyImage, 0, buffer, index, bodyLength);
index += bodyLength;
}
}
int footerLength = FooterLength;
if (footerLength > 0)
{
byte[] footerImage = FooterImage;
if (footerImage is not null)
Buffer.BlockCopy(footerImage, 0, buffer, index, footerLength);
}
return buffer;
}
}
///
/// Gets the binary header image of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual byte[] HeaderImage => null;
///
/// Gets the binary body image of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual byte[] BodyImage => null;
///
/// Gets the binary footer image of the object.
///
///
/// This property is typically overridden by a specific protocol implementation.
///
protected virtual byte[] FooterImage => null;
#endregion
#region [ Methods ]
///
/// Generates binary image of the object and copies it into the given buffer, for bytes.
///
/// Buffer used to hold generated binary image of the source object.
/// 0-based starting index in the to start writing.
/// The number of bytes written to the .
/// is null.
///
/// or is less than 0 -or-
/// and will exceed length.
///
public override int GenerateBinaryImage(byte[] buffer, int startIndex)
{
byte[] image = BinaryImage;
int length = image.Length;
if (length > 0)
{
buffer.ValidateParameters(startIndex, length);
Buffer.BlockCopy(image, 0, buffer, startIndex, length);
}
return length;
}
#endregion
}
}