123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #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
- {
- /// <summary>
- /// 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.
- /// </summary>
- public class MesgDefinition
- {
- #region Fields
- private byte architecture;
- private byte localMesgNum;
- private List<FieldDefinition> fieldDefs = new List<FieldDefinition>();
- #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<FieldDefinition> 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
|