Header.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region Copyright
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // The following FIT Protocol software provided may be used with FIT protocol
  4. // devices only and remains the copyrighted property of Dynastream Innovations Inc.
  5. // The software is being provided on an "as-is" basis and as an accommodation,
  6. // and therefore all warranties, representations, or guarantees of any kind
  7. // (whether express, implied or statutory) including, without limitation,
  8. // warranties of merchantability, non-infringement, or fitness for a particular
  9. // purpose, are specifically disclaimed.
  10. //
  11. // Copyright 2016 Dynastream Innovations Inc.
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // ****WARNING**** This file is auto-generated! Do NOT edit this file.
  14. // Profile Version = 16.60Release
  15. // Tag = production-akw-16.60.00-0-g5d3d436
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #endregion
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using System.IO;
  23. namespace Dynastream.Fit
  24. {
  25. /// <summary>
  26. /// Implements .FIT header encode/decode.
  27. /// </summary>
  28. public class Header
  29. {
  30. #region Fields
  31. private char[] dataType;
  32. private byte size;
  33. #endregion
  34. #region Properties
  35. public byte Size
  36. {
  37. get
  38. {
  39. return size;
  40. }
  41. set
  42. {
  43. if (value == Fit.HeaderWithCRCSize || value == Fit.HeaderWithoutCRCSize)
  44. {
  45. size = value;
  46. }
  47. else
  48. {
  49. throw new FitException("Tried to set Header Size to " + value);
  50. }
  51. }
  52. }
  53. public byte ProtocolVersion { get; set; }
  54. public ushort ProfileVersion { get; set; }
  55. public uint DataSize { get; set; }
  56. public ushort Crc { get; set; }
  57. #endregion
  58. #region Constructors
  59. /// <summary>
  60. /// Build a standard header with CRC. The CRC will be
  61. /// precomputed and it is assumed no data is present yet.
  62. /// </summary>
  63. public Header()
  64. {
  65. Size = Fit.HeaderWithCRCSize;
  66. ProtocolVersion = Fit.ProtocolVersion;
  67. ProfileVersion = Fit.ProfileVersion;
  68. DataSize = 0;
  69. dataType = new char[] { '.', 'F', 'I', 'T' };
  70. UpdateCRC();
  71. }
  72. /// <summary>
  73. /// Build header by decoding callers stream.
  74. /// </summary>
  75. /// <param name="fitStream"></param>
  76. public Header(Stream fitStream)
  77. {
  78. Read(fitStream);
  79. }
  80. #endregion
  81. #region Methods
  82. /// <summary>
  83. /// Verify Header format is valid.
  84. /// </summary>
  85. /// <returns></returns>
  86. public bool IsValid()
  87. {
  88. if (new string(dataType) == ".FIT")
  89. {
  90. // Don't enforce header CRC anymore
  91. return true;
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. /// Populate header object by decoding callers stream
  97. /// </summary>
  98. /// <param name="fitStream">Readable stream</param>
  99. public void Read(Stream fitStream)
  100. {
  101. BinaryReader binReader = new BinaryReader(fitStream);
  102. try
  103. {
  104. Size = binReader.ReadByte();
  105. ProtocolVersion = binReader.ReadByte();
  106. ProfileVersion = binReader.ReadUInt16();
  107. DataSize = binReader.ReadUInt32();
  108. dataType = binReader.ReadChars(4);
  109. if (Size == Fit.HeaderWithCRCSize)
  110. {
  111. Crc = binReader.ReadUInt16();
  112. }
  113. else
  114. {
  115. Crc = 0x0000;
  116. }
  117. }
  118. catch (EndOfStreamException e)
  119. {
  120. throw new FitException("Header:Read() Failed at byte " + fitStream.Position + " - ", e);
  121. }
  122. }
  123. /// <summary>
  124. /// Output header object to beginning of callers writeable stream. Crc should
  125. /// be recalculated before calling.
  126. /// </summary>
  127. /// <param name="fitStream">Writeable, Seekable stream. Position set to end of header</param>
  128. public void Write(Stream fitStream)
  129. {
  130. BinaryWriter bw = new BinaryWriter(fitStream);
  131. bw.BaseStream.Position = 0;
  132. bw.Write(Size);
  133. bw.Write(ProtocolVersion);
  134. bw.Write(ProfileVersion);
  135. bw.Write(DataSize);
  136. bw.Write(dataType);
  137. if (Size == Fit.HeaderWithCRCSize)
  138. {
  139. bw.Write(Crc);
  140. }
  141. }
  142. /// <summary>
  143. /// Recompute the header CRC based on the current contents of the header object
  144. /// </summary>
  145. public void UpdateCRC()
  146. {
  147. using (MemoryStream ms = new MemoryStream())
  148. {
  149. Write(ms);
  150. byte[] headerBuffer = ms.ToArray();
  151. Crc = CRC.Calc16(headerBuffer, headerBuffer.Length - 2);
  152. }
  153. }
  154. #endregion // methods
  155. } // class
  156. } // namespace