ANT_Common.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Linq;
  11. using System.Text;
  12. using System.Runtime.InteropServices;
  13. namespace ANT_Managed_Library
  14. {
  15. /// <summary>
  16. /// This is a static class that manages all the functions and variables common to the whole scope of the library.
  17. /// </summary>
  18. public static class ANT_Common
  19. {
  20. /// <summary>
  21. /// Enables or disables all devices from resetting on startup, shutdown, and on CWTestMode Failure.
  22. /// Default = true.
  23. /// </summary>
  24. static public bool autoResetIsEnabled = true;
  25. #region constants
  26. #if EXT_FUNCTIONALITY
  27. #warning "The Extended build is for internal use only, and even then, only for specific projects, do not use without discussing with the ANT team"
  28. internal const String ANT_UNMANAGED_WRAPPER = "ANT_WrappedLib_Ext.dll";
  29. #else
  30. internal const String ANT_UNMANAGED_WRAPPER = "ANT_WrappedLib.dll";
  31. #endif
  32. internal const String ANT_SI_LIBRARY = "DSI_SiUSBXp_3_1.DLL";
  33. internal const String ANT_SI_LIBRARY2 = "DSI_CP210xManufacturing_3_1.dll";
  34. #endregion
  35. #region DLL imports
  36. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  37. private static extern UInt32 ANT_GetNumDevices();
  38. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  39. private static extern int ANT_EnableDebugLogging();
  40. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  41. private static extern void ANT_DisableDebugLogging();
  42. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
  43. private static extern int ANT_SetDebugLogDirectory(string pcDirectory);
  44. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  45. private static extern int ANT_DebugThreadInit(string pucName);
  46. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  47. private static extern int ANT_DebugThreadWrite(string pcMessage);
  48. [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
  49. internal static extern int ANT_DebugResetTime();
  50. #endregion
  51. /// <summary>
  52. /// Returns the number of ANT USB devices currently detected by the system.
  53. /// </summary>
  54. public static UInt32 getNumDetectedUSBDevices()
  55. {
  56. return ANT_GetNumDevices();
  57. }
  58. /// <summary>
  59. /// Checks if the unmanaged library is present in the application's working directory.
  60. /// Throws an exception if the library is missing.
  61. /// </summary>
  62. public static void checkUnmanagedLibrary()
  63. {
  64. // Check the unmanaged wrapper is present
  65. if (!System.IO.File.Exists(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ANT_UNMANAGED_WRAPPER)))
  66. throw new ANT_Exception(ANT_UNMANAGED_WRAPPER + " not found in working directory");
  67. //We could also check the version of the unmanaged wrapper if there were compatibility issues we wanted to enforce
  68. }
  69. /// <summary>
  70. /// Checks if device specific libraries are present in the application's working directory.
  71. /// Throws an exception if any of these is missing.
  72. /// </summary>
  73. public static void checkUSBLibraries()
  74. {
  75. String missingLibs = null;
  76. // Check for SiLabs libraries
  77. if (!System.IO.File.Exists(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ANT_SI_LIBRARY)))
  78. {
  79. missingLibs = ANT_SI_LIBRARY;
  80. }
  81. if (!System.IO.File.Exists(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ANT_SI_LIBRARY2)))
  82. {
  83. if (missingLibs != null)
  84. {
  85. missingLibs += ", " + ANT_SI_LIBRARY2;
  86. }
  87. else
  88. {
  89. missingLibs = ANT_SI_LIBRARY2;
  90. }
  91. }
  92. if (missingLibs != null)
  93. {
  94. throw new ANT_Exception(missingLibs + " not found in working directory");
  95. }
  96. }
  97. /// <overloads>Enables debug files</overloads>
  98. /// <summary>
  99. /// Initializes and enables debug logs for all devices
  100. /// Note: For application specific logs to work properly
  101. /// (e.g. ANT-FS logs), this needs to be called BEFORE
  102. /// creating an ANT Device.
  103. /// </summary>
  104. public static bool enableDebugLogs()
  105. {
  106. return ANT_EnableDebugLogging() == 1;
  107. }
  108. /// <summary>
  109. /// Initializes and enables debug logs for all devices,
  110. /// and stores the log in the specified path.
  111. /// Note: For application specific logs to work properly
  112. /// (e.g. ANT-FS logs), this needs to be called BEFORE
  113. /// creating an ANT Device.
  114. /// </summary>
  115. /// <param name="debugPath">Debug log directory</param>
  116. public static void enableDebugLogs(string debugPath)
  117. {
  118. enableDebugLogs();
  119. setDebugLogDirectory(debugPath);
  120. }
  121. /// <summary>
  122. /// Disables and closes the debug logs
  123. /// </summary>
  124. public static void disableDebugLogs()
  125. {
  126. ANT_DisableDebugLogging();
  127. }
  128. /// <summary>
  129. /// Set the directory the log files are saved to.
  130. /// This string will prefix the file name so must end with a slash or will be part of the name.
  131. /// ie: directoryPath='c:\ANT\logs' will result in files being saved to the \ANT directory named logsdevice0.txt.
  132. /// Throws an exception if directory does not exist.
  133. /// </summary>
  134. /// <param name="directoryPath">
  135. /// Path to directory to save log files in. Default is the running directory.
  136. /// This string will prefix the file name so must end with a slash or will be part of the name.
  137. /// ie: directoryPath='c:\ANT\logs' will result in files being saved to the \ANT directory named logsdevice0.txt.
  138. /// </param>
  139. public static bool setDebugLogDirectory(string directoryPath)
  140. {
  141. if (!System.IO.Directory.Exists(directoryPath))
  142. throw new ANT_Exception("Path does not exist");
  143. return ANT_SetDebugLogDirectory(directoryPath) == 1;
  144. }
  145. //
  146. //Note: Access to the following logging functions is required by the current ANT-FS implementation
  147. //
  148. /// <summary>
  149. /// Creates a debug log for the currently executing thread
  150. /// </summary>
  151. /// <param name="name">Name of file (will result in ao_debug_name)</param>
  152. /// <returns>True if successful</returns>
  153. internal static bool initDebugLogThread(string name)
  154. {
  155. return ANT_DebugThreadInit(name) == 1;
  156. }
  157. /// <summary>
  158. /// Adds an application specific message to the log for the current thread
  159. /// </summary>
  160. /// <param name="message">Message to write to the log</param>
  161. /// <returns>True on success</returns>
  162. internal static bool writeToDebugLog(string message)
  163. {
  164. return ANT_DebugThreadWrite(message) == 1;
  165. }
  166. }
  167. }