MesgDefinition.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. using Dynastream.Utility;
  24. namespace Dynastream.Fit
  25. {
  26. /// <summary>
  27. /// Architecture defaults to Little Endian (unless decoded from an binary defn as Big Endian)
  28. /// This could be exposed in the future to programatically create BE streams.
  29. /// </summary>
  30. public class MesgDefinition
  31. {
  32. #region Fields
  33. private byte architecture;
  34. private byte localMesgNum;
  35. private List<FieldDefinition> fieldDefs = new List<FieldDefinition>();
  36. #endregion
  37. #region Properties
  38. public ushort GlobalMesgNum { get; set; }
  39. public byte LocalMesgNum
  40. {
  41. get
  42. {
  43. return localMesgNum;
  44. }
  45. set
  46. {
  47. if (value > Fit.LocalMesgNumMask)
  48. {
  49. throw new FitException("MesgDefinition:LocalMesgNum - Invalid Local message number " + value + ". Local message number must be < " + Fit.LocalMesgNumMask);
  50. }
  51. else
  52. {
  53. localMesgNum = value;
  54. }
  55. }
  56. }
  57. public byte NumFields { get; set; }
  58. public bool IsBigEndian
  59. {
  60. get
  61. {
  62. if (architecture == Fit.BigEndian)
  63. {
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. }
  72. #endregion
  73. #region Constructors
  74. internal MesgDefinition()
  75. {
  76. LocalMesgNum = 0;
  77. GlobalMesgNum = (ushort)MesgNum.Invalid;
  78. architecture = Fit.LittleEndian;
  79. }
  80. public MesgDefinition(Stream fitSource)
  81. {
  82. Read(fitSource);
  83. }
  84. public MesgDefinition(Mesg mesg)
  85. {
  86. LocalMesgNum = mesg.LocalNum;
  87. GlobalMesgNum = mesg.Num;
  88. architecture = Fit.LittleEndian;
  89. NumFields = (byte)mesg.fields.Count;
  90. foreach (Field field in mesg.fields)
  91. {
  92. fieldDefs.Add(new FieldDefinition(field));
  93. }
  94. }
  95. public MesgDefinition(MesgDefinition mesgDef)
  96. {
  97. LocalMesgNum = mesgDef.LocalMesgNum;
  98. GlobalMesgNum = mesgDef.GlobalMesgNum;
  99. architecture = mesgDef.IsBigEndian ? Fit.BigEndian : Fit.LittleEndian;
  100. NumFields = mesgDef.NumFields;
  101. foreach (FieldDefinition fieldDef in mesgDef.fieldDefs)
  102. {
  103. fieldDefs.Add(new FieldDefinition(fieldDef));
  104. }
  105. }
  106. #endregion
  107. #region Methods
  108. public void Read(Stream fitSource)
  109. {
  110. fitSource.Position = 0;
  111. EndianBinaryReader br = new EndianBinaryReader(fitSource, false);
  112. LocalMesgNum = (byte)(br.ReadByte() & Fit.LocalMesgNumMask);
  113. byte reserved = br.ReadByte();
  114. architecture = br.ReadByte();
  115. br.IsBigEndian = this.IsBigEndian;
  116. GlobalMesgNum = br.ReadUInt16();
  117. NumFields = br.ReadByte();
  118. for (int i = 0; i < NumFields; i++)
  119. {
  120. FieldDefinition newField = new FieldDefinition();
  121. newField.Num = br.ReadByte();
  122. newField.Size = br.ReadByte();
  123. newField.Type = br.ReadByte();
  124. fieldDefs.Add(newField);
  125. }
  126. }
  127. public void Write(Stream fitDest)
  128. {
  129. BinaryWriter bw = new BinaryWriter(fitDest);
  130. bw.Write((byte)(LocalMesgNum + Fit.MesgDefinitionMask));
  131. bw.Write((byte)Fit.MesgDefinitionReserved);
  132. bw.Write((byte)Fit.LittleEndian);
  133. bw.Write(GlobalMesgNum);
  134. bw.Write(NumFields);
  135. if (NumFields != fieldDefs.Count)
  136. {
  137. throw new FitException("MesgDefinition:Write - Field Count Internal Error");
  138. }
  139. for (int i = 0; i < fieldDefs.Count; i++)
  140. {
  141. bw.Write(fieldDefs[i].Num);
  142. bw.Write(fieldDefs[i].Size);
  143. bw.Write(fieldDefs[i].Type);
  144. }
  145. }
  146. public int GetMesgSize()
  147. {
  148. int mesgSize = 1; // header
  149. foreach (FieldDefinition field in fieldDefs)
  150. {
  151. mesgSize += field.Size;
  152. }
  153. return mesgSize;
  154. }
  155. public void AddField(FieldDefinition field)
  156. {
  157. fieldDefs.Add(field);
  158. }
  159. public void ClearFields()
  160. {
  161. fieldDefs.Clear();
  162. }
  163. public int GetNumFields()
  164. {
  165. return fieldDefs.Count;
  166. }
  167. public List<FieldDefinition> GetFields()
  168. {
  169. // This is a reference to the real list
  170. return fieldDefs;
  171. }
  172. public FieldDefinition GetField(byte num)
  173. {
  174. foreach (FieldDefinition fieldDef in fieldDefs)
  175. {
  176. if (fieldDef.Num == num)
  177. {
  178. return fieldDef;
  179. }
  180. }
  181. return null;
  182. }
  183. public bool Supports(Mesg mesg)
  184. {
  185. return Supports(new MesgDefinition(mesg));
  186. }
  187. public bool Supports(MesgDefinition mesgDef)
  188. {
  189. if (mesgDef == null)
  190. {
  191. return false;
  192. }
  193. if (GlobalMesgNum != mesgDef.GlobalMesgNum)
  194. {
  195. return false;
  196. }
  197. if (LocalMesgNum != mesgDef.LocalMesgNum)
  198. {
  199. return false;
  200. }
  201. foreach (FieldDefinition fieldDef in mesgDef.GetFields())
  202. {
  203. FieldDefinition supportedFieldDef = GetField(fieldDef.Num);
  204. if (supportedFieldDef == null)
  205. {
  206. return false;
  207. }
  208. if (fieldDef.Size > supportedFieldDef.Size)
  209. {
  210. return false;
  211. }
  212. }
  213. return true;
  214. }
  215. #endregion
  216. }
  217. } // namespace