#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;
using Dynastream.Utility;
namespace Dynastream.Fit
{
///
/// Architecture defaults to Little Endian (unless decoded from an binary defn as Big Endian)
/// This could be exposed in the future to programatically create BE streams.
///
public class MesgDefinition
{
#region Fields
private byte architecture;
private byte localMesgNum;
private List fieldDefs = new List();
#endregion
#region Properties
public ushort GlobalMesgNum { get; set; }
public byte LocalMesgNum
{
get
{
return localMesgNum;
}
set
{
if (value > Fit.LocalMesgNumMask)
{
throw new FitException("MesgDefinition:LocalMesgNum - Invalid Local message number " + value + ". Local message number must be < " + Fit.LocalMesgNumMask);
}
else
{
localMesgNum = value;
}
}
}
public byte NumFields { get; set; }
public bool IsBigEndian
{
get
{
if (architecture == Fit.BigEndian)
{
return true;
}
else
{
return false;
}
}
}
#endregion
#region Constructors
internal MesgDefinition()
{
LocalMesgNum = 0;
GlobalMesgNum = (ushort)MesgNum.Invalid;
architecture = Fit.LittleEndian;
}
public MesgDefinition(Stream fitSource)
{
Read(fitSource);
}
public MesgDefinition(Mesg mesg)
{
LocalMesgNum = mesg.LocalNum;
GlobalMesgNum = mesg.Num;
architecture = Fit.LittleEndian;
NumFields = (byte)mesg.fields.Count;
foreach (Field field in mesg.fields)
{
fieldDefs.Add(new FieldDefinition(field));
}
}
public MesgDefinition(MesgDefinition mesgDef)
{
LocalMesgNum = mesgDef.LocalMesgNum;
GlobalMesgNum = mesgDef.GlobalMesgNum;
architecture = mesgDef.IsBigEndian ? Fit.BigEndian : Fit.LittleEndian;
NumFields = mesgDef.NumFields;
foreach (FieldDefinition fieldDef in mesgDef.fieldDefs)
{
fieldDefs.Add(new FieldDefinition(fieldDef));
}
}
#endregion
#region Methods
public void Read(Stream fitSource)
{
fitSource.Position = 0;
EndianBinaryReader br = new EndianBinaryReader(fitSource, false);
LocalMesgNum = (byte)(br.ReadByte() & Fit.LocalMesgNumMask);
byte reserved = br.ReadByte();
architecture = br.ReadByte();
br.IsBigEndian = this.IsBigEndian;
GlobalMesgNum = br.ReadUInt16();
NumFields = br.ReadByte();
for (int i = 0; i < NumFields; i++)
{
FieldDefinition newField = new FieldDefinition();
newField.Num = br.ReadByte();
newField.Size = br.ReadByte();
newField.Type = br.ReadByte();
fieldDefs.Add(newField);
}
}
public void Write(Stream fitDest)
{
BinaryWriter bw = new BinaryWriter(fitDest);
bw.Write((byte)(LocalMesgNum + Fit.MesgDefinitionMask));
bw.Write((byte)Fit.MesgDefinitionReserved);
bw.Write((byte)Fit.LittleEndian);
bw.Write(GlobalMesgNum);
bw.Write(NumFields);
if (NumFields != fieldDefs.Count)
{
throw new FitException("MesgDefinition:Write - Field Count Internal Error");
}
for (int i = 0; i < fieldDefs.Count; i++)
{
bw.Write(fieldDefs[i].Num);
bw.Write(fieldDefs[i].Size);
bw.Write(fieldDefs[i].Type);
}
}
public int GetMesgSize()
{
int mesgSize = 1; // header
foreach (FieldDefinition field in fieldDefs)
{
mesgSize += field.Size;
}
return mesgSize;
}
public void AddField(FieldDefinition field)
{
fieldDefs.Add(field);
}
public void ClearFields()
{
fieldDefs.Clear();
}
public int GetNumFields()
{
return fieldDefs.Count;
}
public List GetFields()
{
// This is a reference to the real list
return fieldDefs;
}
public FieldDefinition GetField(byte num)
{
foreach (FieldDefinition fieldDef in fieldDefs)
{
if (fieldDef.Num == num)
{
return fieldDef;
}
}
return null;
}
public bool Supports(Mesg mesg)
{
return Supports(new MesgDefinition(mesg));
}
public bool Supports(MesgDefinition mesgDef)
{
if (mesgDef == null)
{
return false;
}
if (GlobalMesgNum != mesgDef.GlobalMesgNum)
{
return false;
}
if (LocalMesgNum != mesgDef.LocalMesgNum)
{
return false;
}
foreach (FieldDefinition fieldDef in mesgDef.GetFields())
{
FieldDefinition supportedFieldDef = GetField(fieldDef.Num);
if (supportedFieldDef == null)
{
return false;
}
if (fieldDef.Size > supportedFieldDef.Size)
{
return false;
}
}
return true;
}
#endregion
}
} // namespace