#region Copyright
////////////////////////////////////////////////////////////////////////////////
// The following FIT Protocol software provided may be used with FIT protocol
// devices only and remains the copyrighted property of Dynastream Innovations Inc.
// The software is being provided on an "as-is" basis and as an accommodation,
// and therefore all warranties, representations, or guarantees of any kind
// (whether express, implied or statutory) including, without limitation,
// warranties of merchantability, non-infringement, or fitness for a particular
// purpose, are specifically disclaimed.
//
// Copyright 2016 Dynastream Innovations Inc.
////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 16.60Release
// Tag = production-akw-16.60.00-0-g5d3d436
////////////////////////////////////////////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
namespace Dynastream.Fit
{
///
/// Implements .FIT header encode/decode.
///
public class Header
{
#region Fields
private char[] dataType;
private byte size;
#endregion
#region Properties
public byte Size
{
get
{
return size;
}
set
{
if (value == Fit.HeaderWithCRCSize || value == Fit.HeaderWithoutCRCSize)
{
size = value;
}
else
{
throw new FitException("Tried to set Header Size to " + value);
}
}
}
public byte ProtocolVersion { get; set; }
public ushort ProfileVersion { get; set; }
public uint DataSize { get; set; }
public ushort Crc { get; set; }
#endregion
#region Constructors
///
/// Build a standard header with CRC. The CRC will be
/// precomputed and it is assumed no data is present yet.
///
public Header()
{
Size = Fit.HeaderWithCRCSize;
ProtocolVersion = Fit.ProtocolVersion;
ProfileVersion = Fit.ProfileVersion;
DataSize = 0;
dataType = new char[] { '.', 'F', 'I', 'T' };
UpdateCRC();
}
///
/// Build header by decoding callers stream.
///
///
public Header(Stream fitStream)
{
Read(fitStream);
}
#endregion
#region Methods
///
/// Verify Header format is valid.
///
///
public bool IsValid()
{
if (new string(dataType) == ".FIT")
{
// Don't enforce header CRC anymore
return true;
}
return false;
}
///
/// Populate header object by decoding callers stream
///
/// Readable stream
public void Read(Stream fitStream)
{
BinaryReader binReader = new BinaryReader(fitStream);
try
{
Size = binReader.ReadByte();
ProtocolVersion = binReader.ReadByte();
ProfileVersion = binReader.ReadUInt16();
DataSize = binReader.ReadUInt32();
dataType = binReader.ReadChars(4);
if (Size == Fit.HeaderWithCRCSize)
{
Crc = binReader.ReadUInt16();
}
else
{
Crc = 0x0000;
}
}
catch (EndOfStreamException e)
{
throw new FitException("Header:Read() Failed at byte " + fitStream.Position + " - ", e);
}
}
///
/// Output header object to beginning of callers writeable stream. Crc should
/// be recalculated before calling.
///
/// Writeable, Seekable stream. Position set to end of header
public void Write(Stream fitStream)
{
BinaryWriter bw = new BinaryWriter(fitStream);
bw.BaseStream.Position = 0;
bw.Write(Size);
bw.Write(ProtocolVersion);
bw.Write(ProfileVersion);
bw.Write(DataSize);
bw.Write(dataType);
if (Size == Fit.HeaderWithCRCSize)
{
bw.Write(Crc);
}
}
///
/// Recompute the header CRC based on the current contents of the header object
///
public void UpdateCRC()
{
using (MemoryStream ms = new MemoryStream())
{
Write(ms);
byte[] headerBuffer = ms.ToArray();
Crc = CRC.Calc16(headerBuffer, headerBuffer.Length - 2);
}
}
#endregion // methods
} // class
} // namespace