ANTFS_DeviceParameters.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using System.Runtime.InteropServices;
  12. namespace ANT_Managed_Library.ANTFS
  13. {
  14. /// <summary>
  15. /// ANT-FS Device Parameters
  16. /// </summary>
  17. [StructLayout(LayoutKind.Sequential, Pack=1)]
  18. public struct ANTFS_DeviceParameters
  19. {
  20. /// <summary>
  21. /// Remote device ID
  22. /// </summary>
  23. public uint DeviceID;
  24. /// <summary>
  25. /// Manufacturer ID of remote device
  26. /// </summary>
  27. public ushort ManufacturerID;
  28. /// <summary>
  29. /// Remote device type
  30. /// </summary>
  31. public ushort DeviceType;
  32. /// <summary>
  33. /// Authentication type supported by remote device
  34. /// </summary>
  35. public byte AuthenticationType;
  36. /// <summary>
  37. /// Status byte 1, as described in the ANT-FS Technology specification
  38. /// </summary>
  39. public byte StatusByte1;
  40. /// <summary>
  41. /// Status byte 2, as described in the ANT-FS Technology specification
  42. /// </summary>
  43. public byte StatusByte2;
  44. /// <summary>
  45. /// Configure specified device parameters
  46. /// </summary>
  47. /// <param name="DevID">Device ID</param>
  48. /// <param name="usMfgID">Manufacturer ID</param>
  49. /// <param name="usDevType">Device Type</param>
  50. /// <param name="AuthType">Authentication Type</param>
  51. /// <param name="Stat1">Status byte 1, as described in the ANT-FS Technology specification</param>
  52. /// <param name="Stat2">Status byte 2, as described in the ANT-FS Technology specification</param>
  53. public ANTFS_DeviceParameters(uint DevID, ushort usMfgID, ushort usDevType, byte AuthType, byte Stat1, byte Stat2)
  54. {
  55. DeviceID = DevID;
  56. ManufacturerID = usMfgID;
  57. DeviceType = usDevType;
  58. AuthenticationType = AuthType;
  59. StatusByte1 = Stat1;
  60. StatusByte2 = Stat2;
  61. }
  62. /// <summary>
  63. /// Checks if the remote device is capable of the pairing authentication scheme
  64. /// </summary>
  65. /// <returns>True if pairing is supported, false otherwise</returns>
  66. public bool IsPairingEnabled()
  67. {
  68. if ((StatusByte1 & (byte)Status1.PairingEnabledBit) != 0)
  69. return true;
  70. else
  71. return false;
  72. }
  73. /// <summary>
  74. /// Checks if the remote device supports uploads
  75. /// </summary>
  76. /// <returns>True if uploads are supported, false otherwise</returns>
  77. public bool IsUploadEnabled()
  78. {
  79. if ((StatusByte1 & (byte)Status1.UploadEnabledBit) != 0)
  80. return true;
  81. else
  82. return false;
  83. }
  84. /// <summary>
  85. /// Checks if the remote device has data available for download
  86. /// </summary>
  87. /// <returns>True if data is available, false otherwise</returns>
  88. public bool IsDataAvailable()
  89. {
  90. if ((StatusByte1 & (byte)Status1.DataAvailableBit) != 0)
  91. return true;
  92. else
  93. return false;
  94. }
  95. /// <summary>
  96. /// Obtains the beacon period of the remote device
  97. /// </summary>
  98. /// <returns>Beacon period of the remote device</returns>
  99. public BeaconPeriod GetBeaconPeriod()
  100. {
  101. return (BeaconPeriod)(StatusByte1 & (byte)Status1.BeaconPeriodBits);
  102. }
  103. /// <summary>
  104. /// Obtains current state of the remote device
  105. /// </summary>
  106. /// <returns>State of the remote device</returns>
  107. public ClientState GetClientState()
  108. {
  109. return (ClientState) (StatusByte2 & (byte)Status2.ClientStateBits);
  110. }
  111. /// <summary>
  112. /// Enables/disables the pairing bit in the device parameters
  113. /// </summary>
  114. /// <param name="bEnable">Set to true to enable pairing, false otherwise</param>
  115. public void SetPairingBit(bool bEnable)
  116. {
  117. if (bEnable)
  118. StatusByte1 |= (byte)Status1.PairingEnabledBit;
  119. else
  120. StatusByte1 &= ((byte)Status1.PairingEnabledBit ^ 0xFF); // ~Status1.PairingEnabledBit => ~ Operator converts the value to an Int32
  121. }
  122. /// <summary>
  123. /// Enables/disables the upload bit in the device parameters
  124. /// </summary>
  125. /// <param name="bEnable">Set to true to enable uploads, false otherwise</param>
  126. public void SetUploadBit(bool bEnable)
  127. {
  128. if (bEnable)
  129. StatusByte1 |= (byte)Status1.UploadEnabledBit;
  130. else
  131. StatusByte1 &= ((byte)Status1.UploadEnabledBit ^ 0xFF); // ~Status1.UploadEnabledBit => ~ Operator converts the value to an Int32
  132. }
  133. /// <summary>
  134. /// Enables/disables the data available bit in the device parameters
  135. /// </summary>
  136. /// <param name="bEnable">Set to true if there is data available for download, false otherwise</param>
  137. public void SetDataAvailableBit(bool bEnable)
  138. {
  139. if (bEnable)
  140. StatusByte1 |= (byte)Status1.DataAvailableBit;
  141. else
  142. StatusByte1 &= ((byte)Status1.DataAvailableBit ^ 0xFF); // ~Status1.DataAvailableBit => ~ Operator converts the value to an Int32
  143. }
  144. /// <summary>
  145. /// Returns a string with the decoded device parameters
  146. /// </summary>
  147. /// <returns>String with decoded device parameters</returns>
  148. public override string ToString()
  149. {
  150. String strResults = "";
  151. strResults += " Device ID: " + DeviceID + Environment.NewLine;
  152. strResults += " Manufacturer ID: " + ManufacturerID + Environment.NewLine;
  153. strResults += " Device Type: " + DeviceType + Environment.NewLine;
  154. strResults += " Authentication Type: " + ((AuthenticationType)AuthenticationType).ToString() + Environment.NewLine;
  155. if (IsDataAvailable())
  156. strResults += " Device has new data" + Environment.NewLine;
  157. else
  158. strResults += " Device does not have new data" + Environment.NewLine;
  159. if (IsPairingEnabled())
  160. strResults += " Device is in pairing mode" + Environment.NewLine;
  161. else
  162. strResults += " Device is not in pairing mode" + Environment.NewLine;
  163. if (IsUploadEnabled())
  164. strResults += " Device has upload enabled" + Environment.NewLine;
  165. else
  166. strResults += " Device does not have upload enabled" + Environment.NewLine;
  167. return strResults;
  168. }
  169. }
  170. /// <summary>
  171. /// Parameters retrieved by the host after finding a client matching its search criteria
  172. /// </summary>
  173. public class ANTFS_SearchResults
  174. {
  175. /// <summary>
  176. /// Remote device parameters
  177. /// </summary>
  178. public ANTFS_DeviceParameters DeviceParameters;
  179. /// <summary>
  180. /// Friendly name of the remote device
  181. /// </summary>
  182. public String FriendlyName = "";
  183. /// <summary>
  184. /// Returns a string with the decoded device parameters and friendly name
  185. /// </summary>
  186. /// <returns>String with decoded device parameters and friendly name</returns>
  187. public override string ToString()
  188. {
  189. String strResults = "";
  190. strResults += "Found remote device: " + FriendlyName + Environment.NewLine;
  191. strResults += DeviceParameters.ToString();
  192. return strResults;
  193. }
  194. }
  195. }