PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Synchrophasor/Current Version/Source/Libraries/TVA.PhasorProtocols/Macrodyne/HeaderFrame.cs

#
C# | 236 lines | 106 code | 26 blank | 104 comment | 6 complexity | d4d7cfb91b238a485a386c6261e095d4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, EPL-1.0
  1. //******************************************************************************************************
  2. // HeaderFrame.cs - Gbtc
  3. //
  4. // Copyright Š 2012, Grid Protection Alliance. All Rights Reserved.
  5. //
  6. // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
  7. // the NOTICE file distributed with this work for additional information regarding copyright ownership.
  8. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
  9. // not use this file except in compliance with the License. You may obtain a copy of the License at:
  10. //
  11. // http://www.opensource.org/licenses/eclipse-1.0.php
  12. //
  13. // Unless agreed to in writing, the subject software distributed under the License is distributed on an
  14. // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
  15. // License for the specific language governing permissions and limitations.
  16. //
  17. // Code Modification History:
  18. // ----------------------------------------------------------------------------------------------------
  19. // 02/08/2010 - James R. Carroll
  20. // Generated original version of source code.
  21. //
  22. //******************************************************************************************************
  23. using System;
  24. using System.Runtime.Serialization;
  25. using TVA.IO.Checksums;
  26. using TVA.Parsing;
  27. namespace TVA.PhasorProtocols.Macrodyne
  28. {
  29. /// <summary>
  30. /// Represents the Macrodyne implementation of a <see cref="IHeaderFrame"/> that can be sent or received.
  31. /// </summary>
  32. [Serializable()]
  33. public class HeaderFrame : HeaderFrameBase, ISupportSourceIdentifiableFrameImage<SourceChannel, FrameType>
  34. {
  35. #region [ Members ]
  36. // Fields
  37. private CommonFrameHeader m_frameHeader;
  38. #endregion
  39. #region [ Constructors ]
  40. /// <summary>
  41. /// Creates a new <see cref="HeaderFrame"/>.
  42. /// </summary>
  43. /// <remarks>
  44. /// This constructor is used by a consumer or by <see cref="FrameImageParserBase{TTypeIdentifier,TOutputType}"/> to generate or parse a Macrodyne header frame.
  45. /// </remarks>
  46. public HeaderFrame()
  47. : base(new HeaderCellCollection(10))
  48. {
  49. }
  50. /// <summary>
  51. /// Creates a new <see cref="HeaderFrame"/>.
  52. /// </summary>
  53. /// <param name="headerData"><see cref="string"/> based data to include in this <see cref="HeaderFrame"/>.</param>
  54. /// <remarks>
  55. /// This constructor is used by a consumer to generate a Macrodyne header frame.
  56. /// </remarks>
  57. public HeaderFrame(string headerData)
  58. : this()
  59. {
  60. base.HeaderData = headerData;
  61. }
  62. /// <summary>
  63. /// Creates a new <see cref="HeaderFrame"/> from serialization parameters.
  64. /// </summary>
  65. /// <param name="info">The <see cref="SerializationInfo"/> with populated with data.</param>
  66. /// <param name="context">The source <see cref="StreamingContext"/> for this deserialization.</param>
  67. protected HeaderFrame(SerializationInfo info, StreamingContext context)
  68. : base(info, context)
  69. {
  70. }
  71. #endregion
  72. #region [ Properties ]
  73. /// <summary>
  74. /// Gets the identifier that is used to identify the Macrodyne frame.
  75. /// </summary>
  76. public FrameType TypeID
  77. {
  78. get
  79. {
  80. return Macrodyne.FrameType.HeaderFrame;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets current <see cref="CommonFrameHeader"/>.
  85. /// </summary>
  86. public CommonFrameHeader CommonHeader
  87. {
  88. get
  89. {
  90. // Make sure frame header exists - using base class timestamp to
  91. // prevent recursion (m_frameHeader doesn't exist yet)
  92. if (m_frameHeader == null)
  93. m_frameHeader = new CommonFrameHeader();
  94. return m_frameHeader;
  95. }
  96. set
  97. {
  98. m_frameHeader = value;
  99. if (m_frameHeader != null)
  100. State = m_frameHeader.State as IHeaderFrameParsingState;
  101. }
  102. }
  103. // This interface implementation satisfies ISupportFrameImage<FrameType>.CommonHeader
  104. ICommonHeader<FrameType> ISupportFrameImage<FrameType>.CommonHeader
  105. {
  106. get
  107. {
  108. return CommonHeader;
  109. }
  110. set
  111. {
  112. CommonHeader = value as CommonFrameHeader;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets the length of the <see cref="HeaderFrame"/>.
  117. /// </summary>
  118. /// <remarks>
  119. /// This property is overriden so the length can be extended to include a 1-byte checksum.
  120. /// </remarks>
  121. public override int BinaryLength
  122. {
  123. get
  124. {
  125. // We override normal binary length so we can extend length to include checksum.
  126. // Also, if frame length was parsed from stream header - we use that length
  127. // instead of the calculated length...
  128. if (ParsedBinaryLength > 0)
  129. return ParsedBinaryLength;
  130. // Subtract one byte for Macrodyne 1-byte CRC
  131. return base.BinaryLength - 1;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the length of the <see cref="HeaderImage"/>.
  136. /// </summary>
  137. protected override int HeaderLength
  138. {
  139. get
  140. {
  141. return CommonFrameHeader.FixedLength;
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the binary header image of the <see cref="DataFrame"/> object.
  146. /// </summary>
  147. protected override byte[] HeaderImage
  148. {
  149. get
  150. {
  151. return CommonHeader.BinaryImage;
  152. }
  153. }
  154. #endregion
  155. #region [ Methods ]
  156. /// <summary>
  157. /// Parses the binary header image.
  158. /// </summary>
  159. /// <param name="buffer">Binary image to parse.</param>
  160. /// <param name="startIndex">Start index into <paramref name="buffer"/> to begin parsing.</param>
  161. /// <param name="length">Length of valid data within <paramref name="buffer"/>.</param>
  162. /// <returns>The length of the data that was parsed.</returns>
  163. protected override int ParseHeaderImage(byte[] buffer, int startIndex, int length)
  164. {
  165. // We already parsed the frame header, so we just skip past it...
  166. return CommonFrameHeader.FixedLength;
  167. }
  168. /// <summary>
  169. /// Determines if checksum in the <paramref name="buffer"/> is valid.
  170. /// </summary>
  171. /// <param name="buffer">Buffer image to validate.</param>
  172. /// <param name="startIndex">Start index into <paramref name="buffer"/> to perform checksum.</param>
  173. /// <returns>Flag that determines if checksum over <paramref name="buffer"/> is valid.</returns>
  174. /// <remarks>
  175. /// Default implementation expects 2-byte big-endian ordered checksum. So we override method since checksum
  176. /// in Macrodyne is a single byte.
  177. /// </remarks>
  178. protected override bool ChecksumIsValid(byte[] buffer, int startIndex)
  179. {
  180. int sumLength = BinaryLength - 2;
  181. return buffer[startIndex + BinaryLength - 1] == CalculateChecksum(buffer, startIndex + 1, sumLength);
  182. }
  183. /// <summary>
  184. /// Appends checksum onto <paramref name="buffer"/> starting at <paramref name="startIndex"/>.
  185. /// </summary>
  186. /// <param name="buffer">Buffer image on which to append checksum.</param>
  187. /// <param name="startIndex">Index into <paramref name="buffer"/> where checksum should be appended.</param>
  188. /// <remarks>
  189. /// Default implementation encodes checksum in big-endian order and expects buffer size large enough to accomodate
  190. /// 2-byte checksum representation. We override this method since checksum in Macrodyne is a single byte.
  191. /// </remarks>
  192. protected override void AppendChecksum(byte[] buffer, int startIndex)
  193. {
  194. buffer[startIndex] = (byte)CalculateChecksum(buffer, 1, startIndex);
  195. }
  196. /// <summary>
  197. /// Calculates checksum of given <paramref name="buffer"/>.
  198. /// </summary>
  199. /// <param name="buffer">Buffer image over which to calculate checksum.</param>
  200. /// <param name="offset">Start index into <paramref name="buffer"/> to calculate checksum.</param>
  201. /// <param name="length">Length of data within <paramref name="buffer"/> to calculate checksum.</param>
  202. /// <returns>Checksum over specified portion of <paramref name="buffer"/>.</returns>
  203. protected override ushort CalculateChecksum(byte[] buffer, int offset, int length)
  204. {
  205. // Macrodyne uses 8-bit Xor checksum for frames
  206. return buffer.Xor8CheckSum(offset, length);
  207. }
  208. #endregion
  209. }
  210. }